【问题标题】:how to manage vue reactivity?如何管理 Vue 反应性?
【发布时间】:2019-05-08 10:15:49
【问题描述】:

从数据库获取数据后,我需要更新一个组件。 但我收到一条错误消息。我已经尝试过 v-if 和 v-show 并且得到了相同的结果。

我要更新的组件是modal

<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'”

我希望解释清楚

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    您的回复中没有post.postType,但您正在从模板访问post.postType.id

    <ArticlesEditor v-show=" post.id && post.postType.id"> 
    

    这里,post.postType = undefined & 试图访问 post.postType.id 所以,错误:

    [Vue 警告]:渲染错误:“TypeError: Cannot read property 'id' of undefined”

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 2021-09-03
      • 2020-03-31
      • 2021-10-23
      • 1970-01-01
      • 2021-06-04
      • 2018-04-30
      • 2019-02-05
      • 2017-11-02
      相关资源
      最近更新 更多