Vue.js 如何在存储区对象中访问父vue页面?
在我的vue 2.6/cli 4/vuex 3.1应用程序中,我更新了store vuex中的一些数据,如src/store/index.js中的数据:Vue.js 如何在存储区对象中访问父vue页面?,vue.js,vuejs2,vuex,vee-validate,Vue.js,Vuejs2,Vuex,Vee Validate,在我的vue 2.6/cli 4/vuex 3.1应用程序中,我更新了store vuex中的一些数据,如src/store/index.js中的数据: userPropStore(context, userProp ) { bus.$emit( 'beforeUserPropStore', userProp ); let apiUrl = process.env.VUE_APP_API_URL Vue.http.post(apiUrl + '/personal/user
userPropStore(context, userProp ) {
bus.$emit( 'beforeUserPropStore', userProp );
let apiUrl = process.env.VUE_APP_API_URL
Vue.http.post(apiUrl + '/personal/user-props', userProp).then(({data}) => {
let userProps= this.getters.userProps
userProps.push({
"id": data.user_props.id,
"name": data.user_props.name,
"value": data.user_props.value,
"created_at": data.user_props.created_at,
})
this.commit('refreshUserProps', userProps);
bus.$emit( 'onUserPropStoreSuccess', data );
}, error => {
console.log('context::')
console.log(context)
console.error(error)
self.$refs.userObserverForm.setErrors(error.body.errors)
});
}, // userPropStore(context, userProp ) {
使用vee validate 3.2的ValidationProvider,我希望捕获服务器错误,例如非唯一项
但我有一个错误:
index.js?4360:847 Uncaught (in promise) TypeError: Cannot read property 'userObserverForm' of undefined
在线
self.$refs.userObserverForm.setErrors(error.body.errors)
如果在store对象中有一种方法可以访问父页面,可以使用上下文,该上下文具有:
?
谢谢 这是一个非常奇怪的实现 显然,self.$refs.userObserverForm.setErrorserror.body.errors失败,因为您不在组件中,而组件中有$refs可用 在catch块中要做的是在Vuex中设置错误,然后从中读取组件 伪代码如下: 我不明白你的车在干什么。。。我猜您使用它向组件发送数据,但为什么要使用Vuex呢
我过去这样做的方式是返回Vue.http.post生成的承诺,然后在组件中使用故障信息执行您想要的操作:
userPropStore(context, userProp ) {
bus.$emit( 'beforeUserPropStore', userProp );
let apiUrl = process.env.VUE_APP_API_URL
//add return to the line below
return Vue.http.post(apiUrl + '/personal/user-props', userProp).then(({data}) => {
然后在您的组件中:
loadData() {
this.$store.dispatch('userPropStore').catch((error) => {
this.$refs.userObserverForm.setErrors(error.body.errors)
//any other things you want to do "in component" goes here
});
}
loadData() {
this.$store.dispatch('userPropStore').catch((error) => {
this.$refs.userObserverForm.setErrors(error.body.errors)
//any other things you want to do "in component" goes here
});
}