javascript - 如何管理 Vue 反应性?
问题描述
从数据库获取数据后,我需要更新组件。但我收到一条错误消息。我已经尝试过 v-if 和 v-show 并且得到了相同的结果。
我要更新的组件是模态的
<div>
<b-modal id="modal-xl"
ok-title="next"
:size="modalSize"
@ok.prevent="launchContentModal"
@cancel="resetPostModal"
hide-header-close
no-close-on-backdrop>
<add-post :postTitle="postTitle"
:postTopic="postTopic"
:postType="postType">
</add-post>
<add-content v-show="post.id"
:postType="postType">
</add-content>
<ArticlesEditor v-show=" post.id && post.postType.id">
</ArticlesEditor>
</b-modal>
</div>
和数据对象是:
data(){
return {
postTitle:'',
topic:'',
postTopic:[],
postType: {title: 'video',id:1},
user:{},
post:{
id:'',
postTopic:[],
postType:{
title:'',
id:'',
},
},
}
请求方法是:
launchContentModal(){
if(!this.postTitle) this.$root.$emit('noTitleProvided');
else if(!this.postTopic[0]) this.$root.$emit('noTopicProvided');
else {
this.post = { title: this.postTitle, topics:this.postTopic, type: this.postType};
ResourceCenter.save(this.department, this.user, this.post)
.catch(({response}) =>alert(response.data))
.then(({data}) => {
this.post = data;
});
}
},
发送保存请求后,我得到了正确的响应,这是响应对象:
{"title":"sdf","client_id":"12","department_id":"327","type":"ebook","type_id":2,"user_id":668,"updated_at":"2019-05-08 09:15:44","created_at":"2019-05-08 09:15:44","id":59} ```
并在正确获取响应更新后发布对象:
post:{"title":"sdf","client_id":"12","department_id":"327","type":"ebook","type_id":2,"user_id":668,"updated_at":"2019-05-08 09:15:44","created_at":"2019-05-08 09:15:44","id":59}
预期结果:应该更新模态并显示添加内容组件。
错误消息是:[Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性 'id'”
我希望解释清楚
解决方案
您的回复中没有post.postType
,但您正在post.postType.id
从模板访问:
<ArticlesEditor v-show=" post.id && post.postType.id">
在这里,post.postType = undefined
&试图访问post.postType.id
,错误:
[Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性 'id'”
推荐阅读
- discord - 为什么我的会员加入代码无效?
- java - 使用通用 JSON 的属性生成 XML
- visual-studio - 如何防止 React 构建和 npm 将文件添加到项目中?
- vba - 使用 On Error Resume Next 的错误处理未按预期工作
- python - 两种方式/在字典 Python 上反向搜索
- node.js - 什么时候使用自定义连接工厂?
- python-3.x - 替换字符串中每个偶数 x2 次出现的子字符串
- spring-boot - 如何在 Spring Boot OAuth2 中将身份验证转换为 Jwt 对象?
- reactjs - React - 调用 api 并导出值
- postgresql - 更快地找到不同的值