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