Answer the question
In order to leave comments, you need to log in
How to dynamically change api url?
Hello everyone, I have Vuex and I work with api in it, the fact is that I need to work with two api, they are almost the same and in order not to create 1 more file I want to work in 1
1)/api/photos?new= true&page=${ list }&limit=15
2)/api/photos?popular=true&page=${ list }&limit=15
and I want it to be substituted in api when going to page new,
something like this, /api/photos?$ { new / popular }=true&page=${ list }&limit=15 vuex code
export default {
state: {
posts: [],
perPage: 15,
page: 1,
total: 0,
loading: false,
},
getters: {
numPages: state => Math.ceil( state.total / state.perPage ),
},
mutations: {
updateLoading: ( state, loading ) => state.loading = loading,
updatePosts: ( state, { posts, total, page } ) => Object.assign( state, { posts, total, page } ),
},
actions: {
async fetchPosts( { commit }, page ) {
commit('updateLoading', true)
const list = page
// const start = ( page - 1 ) * state.perPage;
// const end = page * state.perPage;
const new__url = `/api/photos?new=true&page=${ list }&limit=15`
try {
const response = await fetch( new__url )
const posts = await response.json()
const total = posts.totalItems
commit( 'updatePosts', { posts, total, list })
} catch (e) {
console.error(e)
}
commit( 'updateLoading', false )
},
}
}
<template>
<div class="home">
<div class="body">
<appheader class="header"/>
<div class="wrapper">
<ol class="tabs__list">
<router-link to="/photo" >
<ul class="link tabs__link" :class="{ active: activeLink === 1 }" >
<p id='item1' @click="activeLink = 1" > New </p>
</ul>
</router-link>
<router-link to="/popular">
<ul class="link tabs__link" :class="{ active: activeLink === 2 }" >
<p id='item1' @click="activeLink = 2" > Popular </p>
</ul>
</router-link>
</ol>
</div>
</div>
<router-view/>
</div>
</template>
<script>
import appheader from '../components/components/Header'
export default {
components: { appheader, },
data () {
return {
activeLink: undefined,
loading: false,
}
},
methods: {
simulateProgress() {
this.loading = true
setTimeout(() => {
this.loading = false
}, 3000)
},
}
}
</script>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question