Vue.js, pass data to component on another route

我的未来我决定 提交于 2019-12-19 04:05:31

问题


Simple task to pass data from one page to another made a real headache to newbie in Vue.js Here is my router

I'm rendering a form on "/" page, which make an API request,

and I just want to pass data to another route page with another component

Is it so hard to do in Vue? It made a real headache for me today. Get data from API, save & send to another component. How can I do it?


回答1:


As Antony said, since you've already enabled props in router/index.js, you can pass parameters in router.push like this:

router.push({
    name: 'events',
    params: {
        items: data
    }
});

then you will be able to receive items as props in EventsDisplay.vue

export default {
    name: 'eventsDisplay',
    props: ['items'],
};



回答2:


If you want to save it globally, you could use an event bus or a VueX store to contain it, then save and load it from this event bus or store when you need to access it.

If you want to pass it between those two components only, and only when accessing the second page from the first one, you can pass properties to the route argument as per this guide: https://router.vuejs.org/en/essentials/passing-props.html




回答3:


Try using Vuex

In Source Component

this.$store.commit('changeDataState', this.$data);
this.$router.push({name: 'user-post-create'});

In Destination Component

created () {
    console.log('state', this.$store);
    this.$store.getters.Data;
}


来源:https://stackoverflow.com/questions/45484684/vue-js-pass-data-to-component-on-another-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!