嘗試在 Vuejs 中對數組進行排序時計算屬性出現問題?

孫達

var example1 = new Vue({
  el: '#example-1',
  data: {
    // sort: item,
    sortKey: 'name',
    checked: false,
    searchString: "",
    items: [{
        price: '1',
        name: 'mm'
      },
      {
        price: '22',
        name: 'aa'
      },
      {
        price: '55',
        name: 'dd'
      },
      {
        price: '77',
        name: 'gg'
      },
      {
        price: '123',
        name: 'kk'
      },
      {
        price: '53',
        name: 'mn'
      },
    ]
  },
  computed: {
    sortedItems: function() {
      let searchString = this.searchString;
      const sortedArray = this.items.sort((a, b) => {
        if (a[this.sortKey] < b[this.sortKey]) return -1
        if (a[this.sortKey] > b[this.sortKey]) return 1
        return 0
      });
      if (!searchString) {
        return sortedArray;
      } else {
        searchString = searchString.trim().toLowerCase();
        const search_array = sortedArray.filter((item) => {
          if (item.name.toLowerCase().indexOf(searchString) !== -1) {
            return item;
          }
        });
        return search_array;
      }
    },
  }
})
<p>sortKey = {{sortKey}}</p>
<li v-for="item in sortedItems">
  <input class="checkbox-align" type="checkbox" :value="item.name" id="productname" v-model="item.checked" /> {{ item.price }} - {{ item.name }}
</li>

<div class="bbb">
  <button @click="sortKey = 'name'">name</buttton><br />
      <button @click="sortKey = 'price'">price</buttton>
  </div>

嘗試在 Vuejs 中對數組進行排序時計算屬性出現問題?我收到錯誤為 ---vue/no-side-effects-in-computed-properties(sortedItem)

我的代碼在 Codepen 中運行良好,但我不確定為什麼在本地執行時它在 vscode 中不起作用。與 ---vue/no-side-effects-in-computed-properties 完全一樣是什麼意思

代碼筆鏈接:- https://codepen.io/dhanunjayt/pen/oNGXjZL

阿瑪羅克茲

您應該修改計算屬性內的數據變量。這就是它拋出那個錯誤的原因。

請看下面修改後的代碼

var example1 = new Vue({
  el: '#example-1',
  data: {
    // sort: item,
    sortKey: 'name',
    checked: false,
    searchString: "",
    items: [{
        price: '1',
        name: 'mm'
      },
      {
        price: '22',
        name: 'aa'
      },
      {
        price: '55',
        name: 'dd'
      },
      {
        price: '77',
        name: 'gg'
      },
      {
        price: '123',
        name: 'kk'
      },
      {
        price: '53',
        name: 'mn'
      },
    ]
  },
  computed: {
    sortedItems: function() {
      let searchString = this.searchString;
      let itemsClone = [...this.items]; // Change added
      const sortedArray = itemsClone.sort((a, b) => {
        if (a[this.sortKey] < b[this.sortKey]) return -1
        if (a[this.sortKey] > b[this.sortKey]) return 1
        return 0
      });
      if (!searchString) {
        return sortedArray;
      } else {
        searchString = searchString.trim().toLowerCase();
        const search_array = sortedArray.filter((item) => {
          if (item.name.toLowerCase().indexOf(searchString) !== -1) {
            return item;
          }
        });
        return search_array;
      }
    },
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="example-1">
<p>sortKey = {{sortKey}}</p>
<li v-for="item in sortedItems">
  <input class="checkbox-align" type="checkbox" :value="item.name" id="productname" v-model="item.checked" /> {{ item.price }} - {{ item.name }}
</li>

<div class="bbb">
  <button @click="sortKey = 'name'">name</buttton><br />
      <button @click="sortKey = 'price'">price</buttton>
  </div>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

嘗試在 Vuejs 中過濾數組時出現問題?

嘗試對數組進行排序時出現問題?

嘗試在 Vuejs 中的 v-for 中選擇一個複選框時出現問題?

當我嘗試使用函數 MAX() 對鍊錶進行排序時出現分段錯誤

嘗試對數據進行分頁時出現無限循環

嘗試從數組拼接元素時出現問題

嘗試製作計算器

嘗試顯示數組時 ForEach 出錯

嘗試訪問正確分配的數組時出現分段錯誤

嘗試計算Java中String中每個字母的出現次數

嘗試從 JSON 數據訪問對像數組的問題

嘗試關聯 Django 中的表

嘗試在數據庫中寫入數據時出現引導問題

嘗試創建包含節點度數的數據框時出現屬性錯誤

嘗試訪問 Hardhat 中的 Solidity 結構數組時出現無效操作碼錯誤

使用 VueJS 對對像數組中的標題進行排序

嘗試保存對象 Id 數組 Mongoose 的問題

嘗試在邊距中顯示 HTML 元素時如何避免出現問題?

嘗試按日期和時間對預訂進行排序 react.js

嘗試使用 pytest 執行測試時出現問題(沒有名為 * 的模塊)

為什麼在嘗試使用數組創建 Scoreboard 對象時出現錯誤?

嘗試在 Typescript 中對函數進行類型檢查

嘗試從 json 解析值時出現 TypeError

嘗試提取 json 時出現 JSONDecodeError

嘗試傳送玩家時出現 DecoderException

嘗試訪問 nativeLibraryDir 時出錯

嘗試根據 Vue 中的字符長度添加類時,計算屬性引用錯誤的“this”

嘗試在畫布(JS)中更新計時器

嘗試使用 numpy 計算導數時出現 Python 形狀(ValueError)錯誤 - diff