如何将路由参数传递给vuex getter?

时间:2020-04-25 20:17:16

标签: vue.js vuex vue-router

我尝试了How to get Vue Router Params in Vuex?,但似乎没有用。

获取器:

getAssetById: state => id => {
      return state.assets.find(asset => asset.id === id)
    }

组件:

<div class="card padding-35 box-shadow ">
        //this on is OK
        {{ assetId }}

        {{ asset }}
      </div>

 computed: {
    assetId() {
      return this.$route.params.id
    },
    asset() {
      //this doesn't work
      return this.$store.getters.getAssetById(assetId)
    }
    asset() {
      //this work
      return this.$store.getters.getAssetById(1)
    }
  }

2 个答案:

答案 0 :(得分:1)

应该早些考虑,转换为int即可解决问题:

assetId() {
      return parseInt(this.$route.params.id)
    },

答案 1 :(得分:0)

认为您需要将收益更改为this.$store.getters.getAssetById(this.assetId)