computed无法得到ajax获取过来的数据 第11.4小节

来源:11-4 购物车全选和商品实时计算功能实现

毛利小萌萌

2018-03-14

import uaside from '@/components/uaside/index.vue'

import morer from '@/components/more/index.vue'


export default {

props: {

},

data() {

return {

shopcars: [],

more: {

word: '加载更多',

variant: 'primary',

disabled: false

},

page: this.$route.query.page ? this.$route.query.page : 1,

pagesize: this.$route.query.pagesize ? this.$route.query.pagesize : 4

}

},

components: {

uaside,

morer

},

mounted() {

this.index()

},

computed: {

getSelected() {

return this.shopcars.filter((shopcar) => {

return shopcar.isSelected == true

})

},

isSelectedAll() {

return true

}

},

methods: {

//获取购物车列表

index() {

this.$http.get('/api/shopcar',{params: {page: this.page, pagesize: this.pagesize }}).then((res) => {

if(res.data.isSuccess){

if(res.data.data.length == 0){

this.more.word = '没有更多了'

this.more.variant = ''

this.more.disabled = true

}else{

this.shopcars = this.shopcars.concat(res.data.data)

}

}else{

alert(res.data.message)

}

})

},

//加载更多

loadMore() {

this.page++

this.index()

},

//全选/全不选购物车

switchAll(){

for(let shopcar of this.shopcars){

if(this.isSelectedAll){

shopcar.isSelected = false

}else{

shopcar.isSelected = true

}

}

},

//编辑购物车

edit(shopcar) {

this.$http.post(`/api/shopcar/${shopcar._id}`,{params: {count: shopcar.count, isSelected: shopcar.isSelected}}).then((res) => {

if(res.data.isSuccess){

}else{

alert(res.data.message)

}

})

},

//删除购物车

del(id) {

if(confirm('确认要删除该条购物车吗?')){

  this.$http.delete(`/api/shopcar/${id}`).then((res) => {

alert(res.data.message)

setTimeout(() => {

window.location.reload()

},1000)

})

}

}

}

}


先通过ax获取购物车数据,填充到data属性的shopcars里,然后在compute中获取shopcars里已经被选中的购物车,但是computed中打印shopcars总是空数组

写回答

1回答

河畔一角

2018-03-14

computed是计算属性,不是一个可以调用的函数。你再群里找我,我帮你处理。或者你对着教程看

0
0

Vue2.6+Node.js+MongoDB 全栈打造商城系统

课程全面升级,Vue2.6+Koa2,从前端入门全栈,让你的未来更宽广

2634 学习 · 924 问题

查看课程