Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js VueJS将计算值从组件传递到父级_Vue.js - Fatal编程技术网

Vue.js VueJS将计算值从组件传递到父级

Vue.js VueJS将计算值从组件传递到父级,vue.js,Vue.js,我制作了一个VueJS组件,它运行一些元素。此UI的结果是用户选择一个值 我在组件的computed中有一个函数,用于在屏幕上显示用户选择的值 如何将此值传递回父VueJS thingy $emit似乎有点像这样,但我看不出我有什么活动 我已经按照建议提出了一个,但现在还没有 在组件中: computed: { selectedCode: function () { var selected = '(No code sele

我制作了一个VueJS组件,它运行一些元素。此UI的结果是用户选择一个值

我在组件的computed中有一个函数,用于在屏幕上显示用户选择的值

如何将此值传递回父VueJS thingy

$emit似乎有点像这样,但我看不出我有什么活动

我已经按照建议提出了一个,但现在还没有

在组件中:

        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>

这里的问题主要是因为发出一个camelCased事件名。因此,正如报告中所述:

如果发出camelCased事件名称:

this.$emit('myEvent')
听烤肉串的版本不会有任何影响:

<!-- Won't work -->
<my-component v-on:my-event="doSomething"></my-component>
然后你可以很容易地听它,就像:

<code-selector v-on:selected-code="codeSelect"></code-selector>
下面是一个工作演示:

Vue.组件“按钮计数器”{ 数据:函数{ 返回{ 计数:0 } }, 模板:“您单击我{{getCount}}}次。”, 计算:{ getCount:函数{ this.$emit'selected-code',this.count; 返回这个.count; } } } 新Vue{ el:myApp, 数据:{}, 方法:{ codeSelectz{ 控制台。清除 console.logz; } } }
这里的问题主要是因为发出一个camelCased事件名。因此,正如报告中所述:

如果发出camelCased事件名称:

this.$emit('myEvent')
听烤肉串的版本不会有任何影响:

<!-- Won't work -->
<my-component v-on:my-event="doSomething"></my-component>
然后你可以很容易地听它,就像:

<code-selector v-on:selected-code="codeSelect"></code-selector>
下面是一个工作演示:

Vue.组件“按钮计数器”{ 数据:函数{ 返回{ 计数:0 } }, 模板:“您单击我{{getCount}}}次。”, 计算:{ getCount:函数{ this.$emit'selected-code',this.count; 返回这个.count; } } } 新Vue{ el:myApp, 数据:{}, 方法:{ codeSelectz{ 控制台。清除 console.logz; } } }
因此,如果HTML中没有使用计算函数的值,则该函数不会运行,但这仍然无法修复它。所以:我肯定是在使用$emit函数,但是在父函数中仍然没有发生任何事情。因此,如果计算函数的值没有在HTML中使用,则该函数不会运行,但这仍然无法修复它。所以:我肯定在使用$emit函数,但在父函数中仍然没有发生任何事情。