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: 'https://reqres.in/api/products/1',
}
},
})
Vue.config.productionTip = false
Vue.config.devtools = false
new Vue({
store,
el: "#app",
computed: {
...Vuex.mapState({
url: ({ generics: { myUrl} }) => myUrl,
}),
},
created() {
axios({
method: 'GET',
url: this.url
}).then((response) => {
console.log(response.data)
})
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vuex "></script><script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.23.0/axios.min.js" integrity="sha512-Idr7xVNnMWCsgBQscTSCivBNWWH30oo/tzYORviOCrLKmBaRxRflm2miNhTFJNVmXvCtzgms5nlJF4az2hiGnA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<div id="app">
{{ url }}
</div>