java子函数的调用_如何在父事件上调用子组件上的函数

回答(8)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

给子组件 ref 并使用 $refs 直接调用子组件上的方法 .

HTML:

Click

JavaScript的:

var ChildComponent = {

template: '

{{value}}
',

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);

}

}

})

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

你所描述的是父母的状态变化 . 你通过道具把它传给了孩子 . 正如你的建议,你会 watch 那个道具 . 当孩子采取行动时,它会通过 emit 通知父母,然后父母可能会再次更改状态 .

var Child = {

template: '

{{counter}}
',

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;

}

}

})

Go

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

您可以使用 $emit 和 $on . 使用@RoyJ代码:

HTML:

Click

JavaScript的:

var Child = {

template: '

{{value}}
',

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);

}

}

})

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果您有时间,请使用Vuex商店直接观察变量(也称为状态)或触发(也称为调度)操作 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

在 create 期间,不喜欢event-bus approach在孩子身上使用 $on 绑定 . 为什么?后续的 create 调用(我正在使用 vue-router )多次绑定消息处理程序 - 导致每条消息有多个响应 .

将道具从父母传给孩子并将物品观察者放在孩子身上的正统解决方案效果更好 . 唯一的问题是孩子只能对 Value 转变采取行动 . 多次传递相同的信息需要某种簿记来强制转换,以便孩子可以接受改变 .

我发现如果我将消息包装在一个数组中,它将始终触发子监视器 - 即使值保持不变 .

家长:

{

data: function() {

msgChild: null,

},

methods: {

mMessageDoIt: function() {

this.msgChild = ['doIt'];

}

}

...

}

儿童:

{

props: ['msgChild'],

watch: {

'msgChild': function(arMsg) {

console.log(arMsg[0]);

}

}

}

HTML:

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

The below example is self explainatory. where refs and events can be used to call function from and to parent and child.

// PARENT

@onChange="childCallBack"

ref="childRef"

:data="moduleData"

/>

Call Method in child

export default {

methods: {

callChild() {

this.$refs.childRef.childMethod('Hi from parent');

},

childCallBack(message) {

console.log('message from child', message);

}

}

};

// CHILD

Call Parent

export default {

methods: {

callParent() {

this.$emit('onChange', 'hi from child');

},

childMethod(message) {

console.log('message from parent', message);

}

}

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我认为我们应该考虑父母使用孩子的方法的必要性 . 事实上,父母不需要关心孩子的方法,但可以将孩子组件视为FSA(有限状态机) . 父母组件控制子组件的状态 . 因此,观察状态更改或仅使用计算功能的解决方案就足够了

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

在子组件上调用方法的简单解耦方法是从子组件中发出处理程序,然后从父组件调用它 .

var Child = {

template: '

{{value}}
',

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)

}

}

})

Click

父级会在必要时跟踪子处理程序函数和调用 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值