简体   繁体   中英

How can I access to computed state from created

How can I access to my url state that I have in computed from created :

computed:{
    ...mapState({
        url: state => state.generics.myUrl,
    }),
},
created: function(){
        axios({
            method: 'GET',
            url: this.url
        }).then((response) => {

        });
    
},

You can create getter or try like following:

computed:{
...mapState({
    url: ({ generics: { myUrl} }) => myUrl,
  }),
},

 const store = new Vuex.Store({ state: { generics: { myUrl: 'url from vuex', } }, }) Vue.config.productionTip = false Vue.config.devtools = false new Vue({ store, el: "#app", computed: { ...Vuex.mapState({ url: ({ generics: { myUrl} }) => myUrl, }), }, created() { console.log('in created: ', this.url) } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <script src="https://unpkg.com/vuex "></script> <div id="app"> {{ url }} </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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