简体   繁体   English

vue,js vue-router 2组件数据不更新

[英]vue,js vue-router 2 component data doesn't update

I'm fairly new to vue.js and trying to build an SPA. 我对vue.js很新,并试图建立一个SPA。 Basicly i define all routes from my backend with an alias to my API endpoints. 基本上,我使用别名到我的API端点定义来自我后端的所有路由。 a lot of them using the same component. 他们中的很多人使用相同的组件。 Data gets fetched with router.beforeEach and vue-resource. 使用router.beforeEach和vue-resource获取数据。

Now when i navigate from a route to another route which share the same template, my router-view doens't get updated. 现在,当我从路由导航到共享相同模板的另一条路线时,我的路由器视图不会更新。

Here's my code: 这是我的代码:

 <script>

var data = {
    content: null
}

const site = {
    template:'<div>{{this.title}}</div>',
    data: function () {
      return data.content.body
    }
}

const home = {
    template:'<div>{{this.title}}</div>',
    data: function () {
      return data.content.body
    }
}

const routes = [
            { path: '/api/12.json', component: home, alias: '/' },
            { path: '/api/4.json', component: site, alias: '/projekte' },
            { path: '/api/5.json', component: site, alias: '/projekte/projekt-1' },
            { path: '/api/7.json', component: site, alias: '/projekte/projekt-2' },
            { path: '/api/6.json', component: site, alias: '/projekte/projekt-3' },
            { path: '/api/8.json', component: site, alias: '/agentur' },
            { path: '/api/9.json', component: site, alias: '/lab' },
            { path: '/api/10.json', component: site, alias: '/kontakt' },
        ]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {

    Vue.http.get(to.matched[0].path).then((response) => {

        data.content = response;
        console.log(data.content);
        next();

    }, (response) => {

        data.content = {
            'body': {
                'title': 'Error 404'
            }
        };
        next();

    });
})

const app = new Vue({
    router

}).$mount('#app')

</script>

Your data object is not part of your Vue component. 您的data对象不是Vue组件的一部分。 It is defined outside of your Vue app. 它是在您的Vue应用程序之外定义的。

Even though your components - home and site returns the data.content.body object, your main data object is not part of Vue's reactivity system. 即使您的组件 - homesite返回data.content.body对象,您的主data对象也不是Vue反应系统的一部分。 Therefore, the changes in that data object are not tracked. 因此,不跟踪该data对象的更改。

You can read more about it here: https://vuejs.org/guide/reactivity.html 您可以在此处阅读更多相关信息: https//vuejs.org/guide/reactivity.html

To ensure that this does not happen, you need to define your data as part of your component itself. 为确保不会发生这种情况,您需要将data定义为组件本身的一部分。 And you need to do your http calls as part of mounted hook on the route component, which has access to this.data of the component. 并且您需要将http调用作为路由组件上mounted挂钩的一部分,该组件可以访问组件的this.data

If you need to share data between components (most likely), then you need to use state management using vuex that allows you to have a shared state for the entire Vue app. 如果您需要在组件之间共享data (最有可能),那么您需要使用vuex进行状态管理,以允许您拥有整个Vue应用程序的共享状态。

You can read more about Vuex here: http://vuex.vuejs.org/en/intro.html 您可以在此处阅读有关Vuex的更多信息: http ://vuex.vuejs.org/en/intro.html

Here's a working example of vue / vue-router / vuex / vue-resource example for API Calls. 以下是API调用的vue / vue-router / vuex / vue-resource示例的工作示例。 Thanks at Mani for the hint i needed. 感谢Mani提供我需要的提示。

const site = {
    template:'<div>{{ content.title }}</div>',
    computed: {
        content (){
            return store.state.routeContent
        }
    }
}

const home = {
    template:'<div>{{ content.title }}</div>',
    computed: {
        content (){
            return store.state.routeContent
        }
    }
}

const notFound = {
    template: '<div>{{ content.title }}</div>',
    computed: {
        content (){
            return store.state.routeContent
        }
    }
}

const routes = [
    { path: '/api/12.json', component: home, alias: '/' },
    { path: '/api/4.json', component: site, alias: '/projekte' },
    { path: '/api/5.json', component: site, alias: '/projekte/projekt-1' },
    { path: '/api/7.json', component: site, alias: '/projekte/projekt-2' },
    { path: '/api/6.json', component: site, alias: '/projekte/projekt-3' },
    { path: '/api/8.json', component: site, alias: '/agentur' },
    { path: '/api/9.json', component: site, alias: '/lab' },
    { path: '/api/10.json', component: site, alias: '/kontakt' },
    { path: '/*', component: notFound }
]

const store = new Vuex.Store({
    state: {
        routeContent: null
    },
    mutations: {
        routeContent (state, payload) {
            state.routeContent = payload
            document.title = payload.title
        }
    }
})

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {

    Vue.http.get(to.matched[0].path).then((response) => {
        store.commit('routeContent', response.body)
        next()

    }, (response) => {
        console.log(response);
        errorObject = {
            'title': 'Error 404'
        },
        store.commit('routeContent', errorObject)
        next()

    });
})

const app = new Vue({
    router

}).$mount('#app')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM