Answer the question
In order to leave comments, you need to log in
How to change data depending on route in vue?
The essence of the problem is this. The site is designed to display information on 3 categories (I will call them conditionally - item). For example, site.com/item1 displays a selection of information for this item, site.com/item2 and site.com/item3 do the same for themselves. All item1,item2,item3 routes use one component to display home.vue (the main page for them must be identical, information from the server with a selection of goods is loaded onto it).
There is already a certain problem, but I solved it through vuex-router-sync like this:
router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter);
import aboutItem from './views/aboutItem';
import pushPage from './views/pushPage';
import errorPage from './views/error-404';
import home from './views/home';
const routes = [
{
path: '/item1',
name: 'item1',
component: home,
},
{
path: '/item2',
name: 'item2',
component: home,
},
{
path: '/item3',
name: 'item3',
component: home,
},
{
path: '',
name: 'redirect',
redirect: {
path: '/'
},
},
{
path: '*',
name: 'error-404',
component: errorPage,
},
];
export const router = new VueRouter({
routes,
mode: 'history'
});
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
url: {
item1: 'item1',
item2: 'item2',
item3: 'item3',
},
content: 'Hello, ',
},
getters: {
getContent(state) {
if (state.route.name === state.url.item1) {
return state.content + 'item1';
}
else if (state.route.name === state.url.item2) {
return state.content + 'item2';
}
else if (state.route.name === state.url.item3) {
return state.content + 'item3';
}
}
}
});
site.com/item1/about-item/123
site.com/item2/about-item/456
site.com/item3/about-item/789
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