账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Vue 计算属性的问题
    17
    0
    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <div id="app"></div> <template id="a"> <table> <thead> <th>序号</th> <th>书籍名称</th> <th>出版日期</th> <th>价格</th> <th>购买数量</th> <th>操作</th> </thead> <tbody> <tr v-for="(book,index) in fliterBooks"> <td>{{index+1}}</td> <td>{{book.bookName}}</td> <td>{{book.date}}</td> <td>{{book.price}}</td> <td> <button :disabled="book.count<=1" @click="decrement(index)">-1</button> <span>{{book.count}}</span> <button @click="increment(index)">+1</button> </td> <td> <button @click="removeBook(index)">删除</button> </td> </tr> </tbody> </table> <h2>总价格:{{totalPrice}}</h2> </template> <script src="../vue3.js"></script> <script> Vue.createApp({ template:"#a", data() { return { books:[ { bookName:"算法导论", date:"2006-9", price:85, count:1 }, { bookName:"编程艺术", date:"2006-2", price:59, count:1 }, { bookName:"编程珠玑", date:"2008-10", price:39, count:1 }, { bookName:"代码大全", date:"2006-3", price:128, count:1 } ] } }, methods:{ decrement(index) { this.books[index].count--; }, increment(index) { this.books[index].count++; }, removeBook(index) { this.books.splice(index,1); } }, computed:{ totalPrice() { return this.books.reduce((pre,cur)=> { return pre+cur.price*cur.count; },0); }, fliterBooks() { return this.books.map(item=>{ const newItem=Object.assign({},item); newItem.price="$"+item.price; console.log(item); return newItem; }); } } }).mount("#app"); </script></body></html>fliterBooks() { return this.books.map(item=>{ const newItem=Object.assign({},item); newItem.price="$"+item.price; console.log(item); return newItem; }); }我就很奇怪,为什么<tr v-for="(book,index) in fliterBooks">为什么这个计算属性,在button点击(+1 -1)的时候,数据也会发生改变,明明没有进入响应式。totalPrice()引用的是books这个数组,可是点击的时候使用的是fliterBooks这个计算属性自己产生的对象,可是他也会发生改变,为什么啊?
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部