Vue.js 无法使用路由器在api调用中从store vuex传递参数
我尝试使用axios get和vuex store中的参数获取数据。 问题是我得到的参数未定义altound在存储中Vue.js 无法使用路由器在api调用中从store vuex传递参数,vue.js,Vue.js,我尝试使用axios get和vuex store中的参数获取数据。 问题是我得到的参数未定义altound在存储中 data() { return { payments: [], destinations: [], }; }, computed: { codcli() { return this.$store.getters.codcli; }, total() { return thi
data() {
return {
payments: [],
destinations: [],
};
},
computed: {
codcli() {
return this.$store.getters.codcli;
},
total() {
return this.$store.getters.orderTotal;
}
},
methods: {
setData(payments, destinations) {
this.payments = payments;
this.destinations = destinations;
}
},
beforeRouteEnter(to, from, next) {
axios
.all([
axios.get(`/api/payments/${this.codcli}/${this.total}`),
axios.get(`/api/getcustomerdestinations/${this.codcli}`)
])
.then(
axios.spread((payments, destinations) => {
next(vm => vm.setData(payments.data, destinations.data));
console.log(payments.data, destinations.data);
})
.catch(error=> console.log(error))
);
}
这是怎么回事?我搞砸了什么?无法理解,因为vuex的getter工作正常
组件的实例尚未创建,当
beforeRouteEnter
被执行,这就是您无法访问的原因
这个
摘自官方:
一种可能的解决方案是将主存储
文件导入该组件,并直接使用它来访问getter,而不是使用计算的
属性
假设此组件和存储文件存在于同一根文件夹级别:
import store from "./store";
然后像这样访问getter:
beforeRouteEnter (to, from, next) {
const codcli = store.getters.codcli;
const total = store.getters.orderTotal;
axios
.all([
axios.get(`/api/payments/${codcli}/${total}`),
axios.get(`/api/getcustomerdestinations/${codcli}`)
])
.then(
axios.spread((payments, destinations) => {
next(vm => vm.setData(payments.data, destinations.data));
console.log(payments.data, destinations.data);
})
.catch(error=> console.log(error))
);
},
组件的实例尚未创建,当
beforeRouteEnter
被执行,这就是您无法访问的原因
这个
摘自官方:
一种可能的解决方案是将主存储
文件导入该组件,并直接使用它来访问getter,而不是使用计算的
属性
假设此组件和存储文件存在于同一根文件夹级别:
import store from "./store";
然后像这样访问getter:
beforeRouteEnter (to, from, next) {
const codcli = store.getters.codcli;
const total = store.getters.orderTotal;
axios
.all([
axios.get(`/api/payments/${codcli}/${total}`),
axios.get(`/api/getcustomerdestinations/${codcli}`)
])
.then(
axios.spread((payments, destinations) => {
next(vm => vm.setData(payments.data, destinations.data));
console.log(payments.data, destinations.data);
})
.catch(error=> console.log(error))
);
},
这完全取决于你想要达到什么样的用户体验。在导航到特定路线之前或之后获取数据都是有效的。如果您决定坚持在导航到该路线之前获取数据,那么您应该向用户显示某种进度/加载指示器。tks。。。你又好又聪明。。。嫁给我!:)哈哈哈,那将是一个非常有趣的故事,在某地见面。如果你来意大利…:)这一切都取决于你想要达到什么样的用户体验。在导航到特定路线之前或之后获取数据都是有效的。如果您决定坚持在导航到该路线之前获取数据,那么您应该向用户显示某种进度/加载指示器。tks。。。你又好又聪明。。。嫁给我!:)哈哈哈,那将是一个非常有趣的故事,在某地见面。如果你来意大利…:)比埃西奥,一个普雷斯托。:)