【问题标题】:Vuejs2 component not updating as parent data changesVuejs2 组件不会随着父数据的变化而更新
【发布时间】:2017-02-27 03:54:54
【问题描述】:

我正在尝试使用 vuejs2 制作一个简单的产品目录。

我所有的数据都存储在一个对象数组中,然后我有这个数据的一个子集,这是产品目录用来显示每个产品的数据。该子集用于分页。

当用户切换页面时,它会清除并将下一组数据推送到数组中。

这会自动使产品组件显示新数据。

我有以下问题,每个产品都有多种颜色,它们以逗号分隔的字符串形式存储,例如(白色、蓝色、红色)

我正在尝试使该信息显示为每个产品旁边的选项下拉列表。

这一直有效,直到我切换到下一页数据,所有其他详细信息都会更新,除了颜色下拉列表,它只反映前一组数据。

我的产品列表存储在一个对象数组中,例如:

 var obj = {
           productID: productID,
           product: title,
           gender: gender,
           colour: colour,
           cost: cost,
           size: size,
           description: description
          }
  productArray.push(obj);

然后我有几个组件来显示这个数据数组:

Vue.component('product-list', {
 template: '<ul id="productList">'+
        '<product :productID="product.productID" v-for="product in products">'+
        '<h4>Colour</h4><colourSelect :colours="product.colour" :productID="product.productID"></colourSelect>' +
        '<h4>Gender</h4><span class="genderSpan"><p v-bind:id="getID(product.productID)">{{product.gender}}</p></span>' +

    '</product></ul>',
data: function() {
    return {
        products:
          paginatedArray

    };
},

Vue.component('colourSelect', {
 props: ['productID', 'colours'],
 template: '<select  v-bind:id="getID()" class="form-control input-xs"><colourOption v-for="colourItem in colourArray"></colourOption></select>',
data: function () { //split string based into array
    var newArray = [];
    var optionsArray = this.colours.split(',');


    for (i = 0; i < optionsArray.length; i++) {
        var obj = {
            colour: optionsArray[i]
        }
        newArray.push(obj)
    }
    return {
        colourArray: newArray

    };
},
methods: {
    getID: function (test) {
        return 'colourSelect' + this.productID;
    }
  }
});

Vue.component('colourOption', {
 props:['options'],
 template: '<option><slot></slot></option>'
});

在 vuejs 的 app 部分中,我有以下方法来进行分页:

        buildPages: function () {

        for (i = 1; i < this.listLength() /this.totalPage ; i++) {
            this.pages.push(i);
        }
        var page = this.currentPage * this.totalPage;

        for (i = page; i < page +  this.totalPage ; i++) {
            paginatedArray.push(productArray[i]);
        }


    },
    listLength: function () {
        var listTotal = productArray.length;
        return listTotal
    },
    changePage: function (number) {
        this.currentPage = number
        var page = this.currentPage * this.totalPage;
        //paginatedArray = [];

        var count = 0;
        for (i = page; i < page + this.totalPage ; i++) {
            if (typeof productArray[i] !== 'undefined') {
                paginatedArray.splice(count, 1, productArray[i])
            }

            count++
        }
    },

productArray 是存储数据的主要数组,paginatedArray 是产品组件处理的数据子集。

问题似乎在 colourSelect 组件中,在其“数据”部分中,它拆分颜色数据并将其作为 colourOption 组件返回到选择中,但不会在 paginatedArray 更改时更新。

但 colourSelect 组件似乎确实传递了正确的数据,因为 getID 方法正确更新。它只是没有重新运行的数据部分。

这是我的第一个 vuejs 网站,有人对此有任何想法吗?

谢谢

【问题讨论】:

    标签: vuejs2


    【解决方案1】:

    您应该将colourArray 设为计算属性,因为组件的data 块只执行一次,以后道具的更改将不会更新colourArray。

    Vue.component('colourSelect', {
      props: ['productID', 'colours'],
      template: '<select  v-bind:id="getID()" class="form-control input-xs"><colourOption v-for="colourItem in colourArray"></colourOption></select>',
      computed:{
        colourArray: function () { //split string based into array
          var newArray = [];
          var optionsArray = this.colours.split(',');
    
    
          for (i = 0; i < optionsArray.length; i++) {
             var obj = {
                colour: optionsArray[i]
            }
            newArray.push(obj)
          }
          return newArray
      }
    },
    methods: {
        getID: function (test) {
            return 'colourSelect' + this.productID;
        }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-05-21
      • 2016-10-27
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      相关资源
      最近更新 更多