2 回答

TA貢獻1850條經(jīng)驗 獲得超11個贊
import { mapState } from 'vuex'
export default {
data () {
return {
localCount: 1
}
},
// mapState 輔助函數(shù)幫助我們生成計算屬
computed: mapState({
// 箭頭函數(shù)可使代碼更簡練
count: state => state.count,
// 傳字符串參數(shù) 'count' 等同于 'state => state.count'
countAlias: 'count',
// 為了能使用 'this'獲取局部狀態(tài),必須使用常規(guī)函數(shù)
countPlusLocalState (state) {
return state.count + this.localCount
},
// 常規(guī) computed, 沒有使用 store的狀態(tài)
localCountAlias () {
return this.localCount
}
})
}

TA貢獻2021條經(jīng)驗 獲得超8個贊
import {mapState} from 'vuex'
export default {
data() {
return { //你這里少寫了
oldData: 0
}
}
computed: {
...mapState(["count"]),
newData(){
return this.oldData + 1;
}
}
}
添加回答
舉報