【发布时间】:2017-01-23 01:01:54
【问题描述】:
我有一个处理所有业务逻辑、ajax、数据等的容器组件。
它有子组件,他将数据和方法作为道具传递。
所以对于这个问题,有一个“CommentForm”组件,它获取一个prop作为提交时调用的函数。
现在一切正常,因为子组件使用数据调用该方法,但现在我有 2 个选项:
- 数据很好,所以清除组件的输入
- 数据错误,显示错误
第二个选项很简单...
但是第一个,我如何告诉子组件清除其内容, 我需要为它创建另一个道具,还是另一种好方法?
** 此代码是我在 Vue.js 中的代码示例,但这是一般问题,例如 react
let Vue = require('vue');
ParentComponent = Vue.extend({
template: `<div>
<CommentForm submitted="myFucntion"></CommentForm>
</div>`,
methods: {
myFucntion: function(text) {
//whatever
//but on sucess I want to clear the child component
}
}
};
let Vue = require('vue');
CommentForm = Vue.extend({
props: {
submitted: {
type: Function,
required: true
}
},
data: function () {
return {
text: ''
}
},
template: `<div>
<input type="text" v-model="text" />
<button @click="submitted(text)" />
</div>`
};
【问题讨论】:
标签: javascript reactjs vue.js vue-component