2017-04-26 54 views
1

我在表單中有一個vue multiselect的vue組件。我希望禁用/啓用此功能,直到填寫了其他兩個字段。從Vue組件中的總線值獲取布爾值

我已經通過總線值在組件內部訪問了該數據,但是當我嘗試使用它時,我從未得到如預期的真或假回答。

我試圖更新disabled屬性與enableKeywords這是由selectedTopic && questionTitle控制,這兩個值都從根或其他部件上,我可以看到在通過Vue公司開發工具組件的根他們的數據進行更新。

複選元器件

<template> 
    <div> 
    <multiselect 
    v-model="internalValue" tag-placeholder="Add this as new tag" placeholder="Search" 
label="name" track-by="id" :options="options" :multiple="true" :disabled="enableKeywords"> 
    </multiselect> 
    </div> 
</template> 

<script> 
    import Multiselect from 'vue-multiselect' 
    export default { 
    components: { Multiselect }, 
    props: ['value'], 
    data() { 
     return { 
     internalValue: this.value, 
     options: [], 
     selectedTopic: null, 
     questionTitle: null, 
     enableKeywords: selectedTopic && questionTitle 
     } 
    }, 
    mounted(){ 
     axios.get('/vuekeywords').then(response => this.options = response.data); 
     bus.$on("send-topic", selectedTopic => this.selectedTopic = selectedTopic); 
     bus.$on("send-title", questionTitle => this.questionTitle = questionTitle); 
    }, 
    watch: { 
     internalValue(v){ 
     bus.$emit('send-keywords', v); 
     this.$emit('input', v); 
     } 
    } 
    } 
</script> 
+0

我認爲這將只是':禁用=' – Bert

+0

太感謝了,這就是我認爲這將是,但我有錯誤的語法「(selectedTopic && questionTitle)!」。我沒有意識到它需要用括號包裝。請添加爲答案,我會接受。乾杯 –

回答

1

爲了disabled是真實的時候要麼你想不填充的屬性,你應該否定布爾,selectedTopic && questionTitle

:disabled="!(selectedTopic && questionTitle)" 
2

總線具有兩個事件附着,$發射& $上。在收到兩個參數 $,然後切換你的enableKeywordstrue

+0

因此,使用帶if語句的方法來確定enableKeywords是true還是false?謝謝 –

+0

'enableKeywords'由另外兩個元素控制嗎?如果這兩個不是空的,那麼'enableKeywords'是真的。 您可以在$ on事件中將其設置爲true,考慮到您$發出了這兩個其他值 – highFlyingDodo