jbo*_*ily 116 javascript event-handling vue.js vuejs2
在Vue 2.0中,文档和其他人清楚地表明,从父母到孩子的沟通是通过道具进行的.
父母如何告诉孩子一个事件是通过道具发生的?
我应该只看一个名为事件的道具吗?这感觉不对,也没有替代方案($emit
/ $on
是孩子到父母,而中心模型是远程元素).
我有一个父容器,它需要告诉它的子容器,可以在API上使用某些操作.我需要能够触发功能.
joe*_*ick 153
给子组件a ref
并使用$refs
直接调用子组件上的方法.
HTML:
<div id="app">
<child-component ref="childComponent"></child-component>
<button @click="click">Click</button>
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
var ChildComponent = {
template: '<div>{{value}}</div>',
data: function () {
return {
value: 0
};
},
methods: {
setValue: function(value) {
this.value = value;
}
}
}
new Vue({
el: '#app',
components: {
'child-component': ChildComponent
},
methods: {
click: function() {
this.$refs.childComponent.setValue(2.0);
}
}
})
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅参考文献中的Vue文档.
Roy*_*y J 62
你所描述的是父母的状态变化.你通过道具把它传给了孩子.正如你的建议,你会watch
支持.当孩子采取行动时,它会通过a通知父母emit
,然后父母可能会再次改变状态.
var Child = {
template: '<div>{{counter}}</div>',
props: ['canI'],
data: function () {
return {
counter: 0
};
},
watch: {
canI: function () {
if (this.canI) {
++this.counter;
this.$emit('increment');
}
}
}
}
new Vue({
el: '#app',
components: {
'my-component': Child
},
data: {
childState: false
},
methods: {
permitChild: function () {
this.childState = true;
},
lockChild: function () {
this.childState = false;
}
}
})
Run Code Online (Sandbox Code Playgroud)
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.2.1/vue.js"></script>
<div id="app">
<my-component :can-I="childState" v-on:increment="lockChild"></my-component>
<button @click="permitChild">Go</button>
</div>
Run Code Online (Sandbox Code Playgroud)
如果你真的想要将事件传递给孩子,你可以通过创建一个总线(它只是一个Vue实例)并将其作为道具传递给孩子来实现.
dri*_*nor 26
你可以使用$emit
和$on
.使用@RoyJ代码:
HTML:
<div id="app">
<my-component></my-component>
<button @click="click">Click</button>
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
var Child = {
template: '<div>{{value}}</div>',
data: function () {
return {
value: 0
};
},
methods: {
setValue: function(value) {
this.value = value;
}
},
created: function() {
this.$parent.$on('update', this.setValue);
}
}
new Vue({
el: '#app',
components: {
'my-component': Child
},
methods: {
click: function() {
this.$emit('update', 7);
}
}
})
Run Code Online (Sandbox Code Playgroud)
运行示例:https://jsfiddle.net/rjurado/m2spy60r/1/
nil*_*arp 13
在子组件上调用方法的一种简单解耦方法是从子组件发出处理程序,然后从父组件调用它。
var Child = {
template: '<div>{{value}}</div>',
data: function () {
return {
value: 0
};
},
methods: {
setValue(value) {
this.value = value;
}
},
created() {
this.$emit('handler', this.setValue);
}
}
new Vue({
el: '#app',
components: {
'my-component': Child
},
methods: {
setValueHandler(fn) {
this.setter = fn
},
click() {
this.setter(70)
}
}
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<div id="app">
<my-component @handler="setValueHandler"></my-component>
<button @click="click">Click</button>
</div>
Run Code Online (Sandbox Code Playgroud)
父进程跟踪子处理程序函数并在必要时调用。
不喜欢的事件总线方式使用$on
绑定在期间孩子create
。为什么?后续create
调用(我正在使用vue-router
)不止一次绑定消息处理程序——导致每条消息有多个响应。
通过道具下来从父到子,并把财产观察者在孩子的正统解决方案工作一点更好。唯一的问题是孩子只能对价值转变采取行动。多次传递相同的消息需要某种簿记来强制转换,以便孩子可以接受更改。
我发现如果我将消息包装在一个数组中,它总是会触发子观察者——即使值保持不变。
家长:
{
data: function() {
msgChild: null,
},
methods: {
mMessageDoIt: function() {
this.msgChild = ['doIt'];
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
孩子:
{
props: ['msgChild'],
watch: {
'msgChild': function(arMsg) {
console.log(arMsg[0]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<parent>
<child v-bind="{ 'msgChild': msgChild }"></child>
</parent>
Run Code Online (Sandbox Code Playgroud)
如果您有时间,请使用Vuex商店直接观察变量(也称为状态)或触发(也称为调度)操作.
在父组件中调用子组件
<component :is="my_component" ref="my_comp"></component>
<v-btn @click="$refs.my_comp.alertme"></v-btn>
Run Code Online (Sandbox Code Playgroud)
在子组件中
mycomp.vue
methods:{
alertme(){
alert("alert")
}
}
Run Code Online (Sandbox Code Playgroud)
下面的例子是自我解释的。其中 refs 和 events 可用于从父和子调用函数。
// PARENT
<template>
<parent>
<child
@onChange="childCallBack"
ref="childRef"
:data="moduleData"
/>
<button @click="callChild">Call Method in child</button>
</parent>
</template>
<script>
export default {
methods: {
callChild() {
this.$refs.childRef.childMethod('Hi from parent');
},
childCallBack(message) {
console.log('message from child', message);
}
}
};
</script>
// CHILD
<template>
<child>
<button @click="callParent">Call Parent</button>
</child>
</template>
<script>
export default {
methods: {
callParent() {
this.$emit('onChange', 'hi from child');
},
childMethod(message) {
console.log('message from parent', message);
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
90281 次 |
最近记录: |