Skip to content

vue 中子组件调用父组件方法最佳实践

javascript
// parent-component
// 将需要被 <Child/> 调用的方法通过 v-bind 传递给 <Child/>
<template>
  <Child :refresh="query"></Child>
</template>
export default {
  methods: {
    query() {
      // ...
    },
  },
};

// child-component
// 在 <Child/> 中通过 props.refresh 调用 <Parent/> 中的方法
<template>
  <div @click="refresh"></div>
</template>
export default {
  props: {
    refresh: {
      type: Function,
      default: () => () => {}
    }
  },
};
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
来发评论吧~
Powered by Waline v3.5.6

Views 5637 , Visitors 3598
Released under the MIT License.