1.写一个简单的子组件main/index.vue:html
<template> <view> </view> </template> <script> export default { data(){ return { } }, onLoad(){ }, methods:{ childMethod() { console.log('childMethod do...') } } } </script> <style> </style>
在子组件中有一个childMethod方法vue
2.在父组件中引用这个子组件的childMethod方法:this
<template> <view class="content"> <mian-index ref="mainindex"></mian-index> <view @tap="dataAction">button</view> </view> </template> <script> import mainindex from '@/pages/main/index/index.vue' export default { data() { return { }; }, components:{ 'mian-index':mainindex }, onLoad(e) { }, methods:{ dataAction:function(){ this.$refs.mainindex.childMethod(); } } } </script> <style scoped> .content{ width:100%; box-sizing: border-box; } </style>
首先,引入子组件文件,而后用ref给子组件一个id标识,而后经过this.$refs.mainindex.childMethod();调用子组件方法spa
转载时请注明出处及相应连接,本文永久地址:http://www.javashuo.com/article/p-otzjpnym-hb.html,谢谢!code