VueJS access child component's data from parent

前端 未结 5 1407
心在旅途
心在旅途 2020-11-28 05:10

I\'m using the vue-cli scaffold for webpack

My Vue component structure/heirarchy currently looks like the following:

  • App
    • PDF Template
        <
5条回答
  •  日久生厌
    2020-11-28 05:14

    For this kind of structure It's good to have some kind of Store.

    VueJS provide solution for that, and It's called Vuex.If you are not ready to go with Vuex, you can create your own simple store.

    Let's try with this

    MarkdownStore.js

    export default {
    
     data: {
       items: []
     },
    
     // Methods that you need, for e.g fetching data from server etc.
    
     fetchData() {
       // fetch logic
     }
    
    }
    

    And now you can use those data everywhere, with importing this Store file

    HomeView.vue

    import MarkdownStore from '../stores/MarkdownStore'
    
    export default {
    
     data() {
       sharedItems: MarkdownStore.data
     },
    
     created() {
       MarkdownStore.fetchData()
     }
    
    }
    

    So that's the basic flow that you could use, If you dont' want to go with Vuex.

提交回复
热议问题