1.寫一個簡單的子組件main/index.vue:
<template> <view> </view> </template> <script> export default { data(){ return { } }, onLoad(){ }, methods:{ childMethod() { console.log('childMethod do...') } } } </script> <style> </style>
在子組件中有一個childMethod方法
2.在父組件中引用這個子組件的childMethod方法:
<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();調用子組件方法
轉載時請注明出處及相應鏈接,本文永久地址:https://www.cnblogs.com/wangxiaoling/p/10250903.html,謝謝!
