2 回答

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

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