2017-03-03 127 views
2

我试图通过道具将组件命名空间传递给组件。当我尝试并映射到与道具干将,它抛出这个错误,当映射到命名空间模块时,将prop作为模块名称

遗漏的类型错误:无法转换未定义或为空反对

如果我通过名称作为它的工作原理字符串。

这个作品

<script> 
export default { 

    props: ['store'], 

    computed: { 
    ...mapGetters('someString', [ 
     'filters' 
    ]) 
    } 
} 
</script> 

这不起作用

this.store定义
this.store的typeof是一个String

<script> 
    export default { 

    props: ['store'], 

    computed: { 
     ...mapGetters(this.store, [ 
     'filters' 
     ]) 
    } 
    } 
</script> 

回答

0

错误你”在Vue/Vuex期间正遭遇重遇初始化过程this.store不能被转换,因为它还不存在。我还没有与命名空间没有工作,这是未经测试,所以我不知道这是否会工作,但你可以解决这个问题,通过有这样一个中介:

<script> 
    export default { 

    props: ['store'], 

    data { 
     namespace: (this.store !== undefined) ? this.store : 'null', 
    }, 

    computed: { 
     ...mapGetters(this.namespace, [ 
     'filters' 
     ]) 
    } 
    } 
</script> 

那如果this.store未定义,则三元表达式将返回一个字符串,如果未定义,则它将返回this.store中的值。

+0

谢谢你的想法,我认为你是在正确的轨道上,但你的解决方案并不适合我。我仍然看到同样的错误。 – MrGood