更改数组中对象的属性

关注需要改变状态时,如果是直接的属性可以直接使用this.article.isFollow = !this.article.isFollow;更改 数组中的属性无法直接更改

1573890522(1).jpg

点击一个时需要将所有id为这个的全部改变

// 关注
follow(id, index, focus) {
  if (this.isLogin) {
    // this.$axios.get("/apis/api/ufollow/" + id).then(({ data }) => {
    //   if (data.errno == "success") {
    //     this.article.isFollow = !this.article.isFollow;
    //   }
    // });
    if (focus.is == 0) {
      this.followtype = 0;
    } else {
      this.followtype = 1;
    }
    let data = {
      type: this.followtype
    };
    let _this = this;
    this.$axios.post("/apis/api/follow/" + id, data).then(({ data }) => {
      if (data.errno == "success") {
        this.article.forEach((element, i) => {
          if (id === element.user_id) {
            // let temp = element.user;
            // if (_this.followtype === 0) {
            //   temp.user_follow.push(1);
            // } else if (_this.followtype === 1) {
            //   temp.user_follow = [];
            // }
            // element.user = Object.assign([], temp);
            if(this.followtype == 0){
              element.focus.is = 1
            }else{
              element.focus.is = 0
            }
            // element.focus.is = !.focus.is;
            _this.article[i] = Object.assign({},element);
          }
        });
      }
    });
  } else {
    this.$store.commit("loginType", true);
  }
},

其中的属性无法直接改变(即无法直接使用this.article.focus.is = !this.article.focus.is;)(在forEach数组中this.article无法直接找到内容)将内容赋值给element.focus.is然后将element.focus.is的内容添加给article,在发送请求之前需要定义一个变量代替this

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容