Vue 在子组件中调用方法的最佳实践 [英] Vue best practice for calling a method in a child component
问题描述
我已经阅读了很多关于此的文章,似乎有多种方法可以做到这一点,许多作者建议不要使用某些实现.
为了简单起见,我创建了一个非常简单的版本来实现我想要实现的目标.
我有一个父 Vue,parent.vue.它有一个按钮:
<div><button v-on:click="子XXXX中的XXXXX调用方法">Say Hello</button>
</模板>
在子Vue中,child.vue我有一个带函数的方法:
方法:{问好() {警报('你好');}}
我想在单击父级中的按钮时调用 sayHello()
函数.
我正在寻找执行此操作的最佳实践方法.我看到的建议包括事件总线、子组件引用和道具等.
在我的方法中执行函数的最简单方法是什么?
抱歉,这看起来确实非常简单,但我确实尝试过做一些研究.
谢谢!
您可以创建一个 ref
并访问方法,但不建议这样做.您不应该依赖组件的内部结构.这样做的原因是您将紧密耦合组件,而创建组件的主要原因之一是松散耦合.
您应该依靠 contract
(某些框架/语言中的接口)来实现这一点.Vue
中的 contract
依赖于这样一个事实,即父与子通过 props
通信,children
与 通信父
通过事件
.
当您想在非父/子组件之间进行通信时,还有至少 2 种其他方法可以进行通信:
- 事件总线
- vuex
我现在将描述如何使用道具:
1.在你的子组件上定义
props: ['testProp'],方法: {问好() {警报('你好');}}
2.在父组件上定义一个触发器数据
data() {返回 {触发:0}}
3.在父组件上使用prop
<div><childComponent :testProp="trigger"/>
</模板>
4.在子组件中观察testProp
并调用sayHello
观看:{testProp: 函数(newVal, oldVal) {this.sayHello()}}
5.从父组件更新trigger
.确保始终更改 trigger
的值,否则 watch
不会触发.一种方法是增加触发器,或将其从真值切换为假值 (this.trigger = !this.trigger)
I have been reading lots of articles about this, and it seems that there are multiple ways to do this with many authors advising against some implementations.
To make this simple I have created a really simple version of what I would like to achieve.
I have a parent Vue, parent.vue. It has a button:
<template>
<div>
<button v-on:click="XXXXX call method in child XXXX">Say Hello</button>
</div>
</template>
In the child Vue, child.vue I have a method with a function:
methods: {
sayHello() {
alert('hello');
}
}
I would like to call the sayHello()
function when I click the button in the parent.
I am looking for the best practice way to do this. Suggestions I have seen include Event Bus, and Child Component Refs and props, etc.
What would be the simplest way to just execute the function in my method?
Apologies, this does seem extremely simple, but I have really tried to do some research.
Thanks!
You can create a ref
and access the methods, but this is not recommended. You should't rely on the internal structure of a component. The reason for this is that you'll tightly couple your components and one of the main reasons to create components is to loosely couple them.
You should rely on the contract
(interface in some frameworks/languages) to achieve this. The contract
in Vue
relies on the fact that parent communicate with children via props
and children
communicate with parent
via events
.
There are also at least 2 other methods to communicate when you want to communicate between components that aren't parent/child:
- the event bus
- vuex
I'll describe now how to use a prop:
1.Define it on your child component
props: ['testProp'],
methods: {
sayHello() {
alert('hello');
}
}
2.Define a trigger data on the parent component
data () {
return {
trigger: 0
}
}
3.Use the prop on the parent component
<template>
<div>
<childComponent :testProp="trigger"/>
</div>
</template>
4.Watch testProp
in the child component and call sayHello
watch: {
testProp: function(newVal, oldVal) {
this.sayHello()
}
}
5.Update trigger
from the parent component. Make sure that you always change the value of trigger
, otherwise the watch
won't fire. One way of doing this is to increment trigger, or toggle it from a truthy value to a falsy one ( this.trigger = !this.trigger)
这篇关于Vue 在子组件中调用方法的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!