在VueJS中将父函数传递给子组件
Passing Parent Function to Child Component in VueJS
我正在VueJS 1.0中进行练习,我正在学习组件。在该example
中存在input
元素并且必须从API提供coupon
或某种code
。我必须验证。我有我的<coupon >
组件,并且有when-applied
的道具。when-applied
必须调用父函数setCoupon
,但它不会。
我只得到了这个错误this.whenApplied is not a function
。
<div id="demo" class="list-group">
<script id="coupon-template" type="x-template">
<input type="text" v-model="coupon" v-on:blur="whenCouponHasBeenEntered">
<div v-text="text"></div>
</script>
<coupon when-applied="setCoupon"></coupon>
</div>
这是我的app.js
文件
Vue.component('coupon', {
template: '#coupon-template',
props: ['whenApplied'],
data: function() {
return {
coupon: '',
invalid: false,
text: ''
}
},
methods: {
whenCouponHasBeenEntered: function() {
this.validate();
},
validate: function() {
if( this.coupon == 'FOOBAR') {
this.whenApplied(this.coupon);
return this.text = '20% OFF!!';
}
return this.text = 'that coupon doesn`t exists';
}
}
});
new Vue({
el: '#demo',
methods: {
setCoupon: function(coupon) {
alert('set coupon'+ coupon);
}
}
});
有人请帮忙。非常感谢您的建议。
您应该bind
属性:
<coupon v-bind:when-applied="setCoupon"></coupon>
或者您可以使用v-bind
:的简写语法
<coupon :when-applied="setCoupon"></coupon>
点击此处了解更多关于props
的信息。
相关文章:
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- 如何控制组件'的createContent函数被激发
- 如何在渲染函数中检查 react 中子组件的状态
- ember-js组件初始化不同的函数
- 如何向onClick事件处理程序传递一个接受参数的函数,并且仍然将该函数绑定到组件's”;这个“;上下文
- 带有内置图像对象的Canvas组件构造函数;t显示's图像
- 如何在单击组件时调用函数
- 当写入“;函数“;在ReactJS组件中
- ES6调用扩展组件函数
- 主干调用在 React 组件函数中不起作用
- 在 Knockout 中初始化时调用组件函数
- Requirejs带有Knockout错误:无法处理绑定“;组件:函数(){return f}"-匿名定义()
- ReactJS组件函数
- 反应:“this”在组件函数中是未定义的
- 调用Coldfusion组件函数的jQuery-post方法不起作用
- 配置子组件函数,将多个参数传递给父函数
- 在Reactjs中,从函数内部调用组件函数
- 需要新语法的React/Redux组件函数
- 如何从javascript函数内部调用React组件函数
- 从dangouslysetinnerhtml中的onclick调用React组件函数