Vue.js 从Vue组件中的总线值获取布尔值
我有一个vue组件,用于表单中的。我想禁用/启用此功能,直到填写了另外两个字段 我已经通过组件内部的总线值访问了这些数据,但当我尝试使用它时,我从未得到预期的正确或错误答案 我正在尝试使用Vue.js 从Vue组件中的总线值获取布尔值,vue.js,vuejs2,vue-component,Vue.js,Vuejs2,Vue Component,我有一个vue组件,用于表单中的。我想禁用/启用此功能,直到填写了另外两个字段 我已经通过组件内部的总线值访问了这些数据,但当我尝试使用它时,我从未得到预期的正确或错误答案 我正在尝试使用enableKeywords更新disabled属性,该属性由selectedTopic&&questionTitle控制,这两个值都来自根目录或其他组件,我可以通过Vue开发工具在该组件的根目录中看到它们的数据更新 多选组件 <template> <div> <mul
enableKeywords
更新disabled
属性,该属性由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>
从“vue Multiselect”导入Multiselect
导出默认值{
组件:{Multiselect},
道具:['value'],
数据(){
返回{
内在价值:这个价值,
选项:[],
selectedTopic:空,
问题标题:空,
启用关键字:selectedTopic&&questionTitle
}
},
安装的(){
get('/vuekeywords')。然后(response=>this.options=response.data);
总线.$on(“发送主题”,selectedTopic=>this.selectedTopic=selectedTopic);
总线.$on(“发送标题”,questionTitle=>this.questionTitle=questionTitle);
},
观察:{
内在价值(v){
总线$emit('send-keywords',v);
这.$emit('input',v);
}
}
}
总线连接了两个事件,$emit和$on。
$在收到这两个参数后,将
启用关键字
切换为真
,以便禁用
在您想要的任何属性未填充时为真,您应该否定布尔值,selectedTopic&&questionTitle
:disabled="!(selectedTopic && questionTitle)"
因此,使用带有if语句的方法来确定enableKeywords是true还是false?Thank
enableKeywords
由另外两个元素控制,对吗?如果这两个关键字不为空,则enableKeywords
为真。您可以在$on事件中将其设置为true,考虑到您是$emit这两个其他值,我认为这将是:disabled=“!(selectedTopic&&questionTitle)”
非常感谢,这是我所认为的,但我的语法错误。我没有意识到它需要用括号括起来。请添加作为答案,我将接受。干杯