举例的都是方法,参数的话是大同小异,可以自己动手试试!
- // 父组件
- <template>
- <div>
- <head ref="headchild"/>
- </div>
- </template>
- <script>
- import head from "./head";
-
- export default {
- methods: {
- getList(){
- this.$refs.headchild.init()
- }
- }
- }
- <script/>
-
- // 子组件
- <template>
- <div></div>
- </template>
- <script>
-
- export default {
- methods: {
- init(){
- console.log('zxc')
- }
- }
- }
- <script/>
- // 父组件
- <template>
- <head ref="headchild"/>
- </template>
-
- <script setup>
- import head from "./head";
- import { ref } from "vue";
- const headchild= ref(null);
- const getList = () => {
- headchild.value.init()
- }
- <script/>
-
- // 子组件
- <template>
- <div></div>
- </template>
- <script setup>
- const init = () => {
- console.log('zxc')
- }
- defineExpose({
- init,
- });
- <script/>