Vue中如何详细运用$refs、$emit、$on、$once、$off方法?

更新于
2026-10-02 17:16:58
0阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计710个文字,预计阅读时间需要3分钟。

1. 使用 `$refs` 的场景:父组件调用子组件的方法,传递数据。 父组件: javascript import ChildA from './child.vue'; export default { methods: { getMyEvent() { this.$refs.child.myMethod(); } } }

1.$refs的使用场景

父组件调用子组件的方法,可以传递数据。

父组件:

<div id="app"> <child-a ref="child"></child-a> <button @click="getMyEvent">点击父组件</button> <div> <script> import ChildA from './child.vue' export default{ components:{ ChildA }, data(){ return { msg:'我是父组件的数据' } }, methods:{ getMyEvent(){ //调用子组件的方法,child是上边ref起的名字,emitEvent是子组件的方法。

阅读全文
标签:使用

本文共计710个文字,预计阅读时间需要3分钟。

1. 使用 `$refs` 的场景:父组件调用子组件的方法,传递数据。 父组件: javascript import ChildA from './child.vue'; export default { methods: { getMyEvent() { this.$refs.child.myMethod(); } } }

1.$refs的使用场景

父组件调用子组件的方法,可以传递数据。

父组件:

<div id="app"> <child-a ref="child"></child-a> <button @click="getMyEvent">点击父组件</button> <div> <script> import ChildA from './child.vue' export default{ components:{ ChildA }, data(){ return { msg:'我是父组件的数据' } }, methods:{ getMyEvent(){ //调用子组件的方法,child是上边ref起的名字,emitEvent是子组件的方法。

阅读全文
标签:使用