2017-03-03 75 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