我正在学习Vue.js,并且想要更改子组件的v-model绑定值,然后在父组件中触发它的事件。
我使用的是element ui文档中的demo,我想使用树过滤器组件,但有时我需要直接修改子输入值,但我发现子组件事件有问题。
一切都很好,但是没有子组件事件发生,有什么问题吗?
这是我的child.vue文件
<template>
<div>
<el-input
placeholder="输入关键字进行过滤"
v-model="filterText">
</el-input>
<el-tree
class="filter-tree"
:data="data2"
:props="defaultProps"
default-expand-all
:filter-node-method="filterNode"
ref="tree2">
</el-tree>
</div>
</template>
<script>
export default {
props: ['value']
watch: {
value(val) {
this.filterText = val; // modify filterText value
}
filterText(val) {
// this will call, but not call filterNode
this.$refs.tree2.filter(val);
}
},
methods: {
filterNode(value, data) {
if (!value) return true;
return data.label.indexOf(value) !== -1;
}
},
data() {
return {
filterText: '',
data2: [{
id: 1,
label: '一级 1',
children: [{
id: 4,
label: '二级 1-1',
children: [{
id: 9,
label: '三级 1-1-1'
}, {
id: 10,
label: '三级 1-1-2'
}]
}]
}, {
id: 2,
label: '一级 2',
children: [{
id: 5,
label: '二级 2-1'
}, {
id: 6,
label: '二级 2-2'
}]
}, {
id: 3,
label: '一级 3',
children: [{
id: 7,
label: '二级 3-1'
}, {
id: 8,
label: '二级 3-2'
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
}
};
</script>这是Parent.vue文件
<child v-model="sinput></child>
...
this.sinput = "1"; // change 发布于 2019-04-16 12:42:17
我猜你正在将这个'sinput‘绑定到选定的树节点值上,然后你需要在树组件上使用事件' node -click’。
对于自定义组件上的v-model,您需要在vue中使用'model‘选项。
参考:Vue.js - Customizing Component v-model
这里有一些代码,希望它能有所帮助:)
<template>
<el-tree
...
@node-click="handleNodeClick"
/>
</template>export default {
model: {
prop: 'sinput',
event: 'change'
},
props: {
sinput: String
},
methods: {
handleNodeClick(data) {
if (!data.children) {
// your code
this.$emit('change', data.value);
}
}
}
}https://stackoverflow.com/questions/55700199
复制相似问题