【发布时间】:2020-08-24 14:53:01
【问题描述】:
我制作了一个 VueJS 组件,它可以操作一些 <select> 元素。这个 UI 的结果是用户选择了一个值。
我在组件的computed 中有一个函数,用于在屏幕上显示用户选择的值。
如何将此值传递回父级 VueJS 事物?
$emit 似乎是这样,但我没有看到我有活动。
按照here 的建议,我已经绑定了一个,但现在没有发生。
在组件中:
computed: {
selectedCode: function () {
var selected = '(No code selected.)';
if (this.category) { selected = this.category; }
if (this.code) { selected = this.code; }
this.$emit('selectedCode', selected);
return selected;
},
在父级 Vue 应用中:
<code-selector v-bind:code="code" v-on:selectedCode="codeSelect"></sic-selector>
和
methods:
{
selectedCode: function (z) {
console.log(z);
},
【问题讨论】:
-
因此,如果 HTML 中未使用其值,则计算函数不会运行,但这仍未解决。所以:我肯定会使用
$emit函数,但父级中仍然没有发生任何事情。
标签: vue.js