Framework.vue中通过路由出口
<template>
<div>
framework.vue
<router-view/>
div>
<template>
<script>
import { ref, reactive, getCurrentInstance, nextTick, watch, provide } from 'vue'
provide('testFun',()=>{
console.log('framework testFun()..');
})
script>
<template>
<div class="main">
<el-button @click="testFun">ttel-button>
div>
<template>
<script>
import { ref,reactive, nextTick, inject } from 'vue'
let testFun = inject('testFun')
script>
<template>
<div>
framework.vue
<router-view @kk="kk"/>
div>
<template>
<script>
import { ref, reactive, getCurrentInstance, nextTick, watch, provide } from 'vue'
const kk = () =>{
console.log('kk');
}
script>
<template>
<div class="main">
<el-button @click="testKK">KKel-button>
div>
template>
<script setup>
import { ref,reactive, nextTick, inject } from 'vue'
const emit = defineEmits([
'kk'
])
const testKK = ()=>{
emit('kk')
console.log('testKK') // 先触发父组件的自定义事件, 调用父组件中绑定的方法之后,再执行的此次打印testKK
}
script>
<style lang="scss">style>
<template>
<div>
framework.vue
<router-view v-slot:="{Component,route}">
<component @kk="kk" :is="Component" :key="route.path"/>
router-view>
div>
<template>
<script>
import { ref, reactive, getCurrentInstance, nextTick, watch, provide } from 'vue'
const kk = () =>{
console.log('kk');
}
script>
<template>
<div class="main">
<el-button @click="testKK">KKel-button>
div>
template>
<script setup>
import { ref,reactive, nextTick, inject } from 'vue'
const emit = defineEmits([
'kk'
])
const testKK = ()=>{
emit('kk')
}
script>
<style lang="scss">style>
可参考:vue3 调用router-view下的组件方法
路由子组件,而非在父组件中直接使用的子组件
)<template>
<div>
framework.vue
<el-button @click="triggerJJ">触发路由子组件的jj方法?el-button>
<router-view v-slot:="{Component,route}">
<component ref="routerViewRef" :key="route.path"/>
router-view>
div>
<template>
<script>
import { ref, reactive } from 'vue'
const routerViewRef = ref()
const triggerJJ = () => {
routerViewRef.value.jj()
}
script>
<template>
<div class="main">
main-jj
div>
template>
<script setup>
import { ref,reactive, nextTick, inject } from 'vue'
const jj = ()=>{
console.log('main');
}
defineExpose({
jj
})
script>
<style lang="scss">
style>