鍍金池/ 問答/HTML/ vue的computed參數(shù)可以是function嗎?

vue的computed參數(shù)可以是function嗎?

computed:mapState({
        count:state=>state.count
 })

上面代碼是使用vuex時設置的computed,mapState是一個函數(shù),但是我在vue的官網(wǎng)上看到computed的參數(shù)是一個對象,請問這是為什么?

回答
編輯回答
深記你

請你仔細看看,computed 是個函數(shù)嗎?

computed: mapState()

如果還不明白的話:

function mapState() {
  return {}
}
...
computed: mapState() // computed 是個函數(shù)嗎?

如果還不明白的話,建議你暫停 Vue 的學習,先補一補 javascript 基礎

希望對你有幫助

2017年6月9日 15:22
編輯回答
只愛你

mapState是一個函數(shù)。mapState()是一個函數(shù)嗎?

function fun () {return {}}
fun
fun()
// 這倆啥區(qū)別
2017年10月17日 15:37
編輯回答
心沉

建議你這么寫:

computed: {
      ... mapState([
        'getAddressNumber'
      ]),
      ...
    },

因為computed的用途不只vuex,你那樣寫,別的就不好用了

2017年10月17日 11:13
編輯回答
乖乖噠
import { mapState } from 'vuex'

export default {
  // ...
  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
    }
  })
}

這個是官網(wǎng)的一個例子 有什么區(qū)別?

2017年6月16日 16:42