MrG*_*ood 7 javascript vue.js vue-component vuex vuejs2
我正在尝试通过props将商店模块命名空间传递给组件.当我尝试使用道具映射到吸气剂时,它会抛出此错误,
未捕获的TypeError:无法将undefined或null转换为object
如果我将名称作为字符串传递它是有效的.
这个作品
<script>
export default {
props: ['store'],
computed: {
...mapGetters('someString', [
'filters'
])
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
这不起作用
this.store定义
this.store typeof是一个String
<script>
export default {
props: ['store'],
computed: {
...mapGetters(this.store, [
'filters'
])
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
K3T*_*H3R -1
您遇到的错误是在 Vue/Vuex 的初始化过程中抛出的,this.store无法转换,因为它尚不存在。我还没有使用命名空间,而且这还没有经过测试,所以我不知道它是否会起作用,但您也许可以通过像这样的中介来解决这个问题:
<script>
export default {
props: ['store'],
data {
namespace: (this.store !== undefined) ? this.store : 'null',
},
computed: {
...mapGetters(this.namespace, [
'filters'
])
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果未定义,该三元表达式将返回一个字符串this.store,如果不是未定义,则它将返回 中的值this.store。