演示地址:http://39.106.81.114:8888
main.js
import * as echarts from 'echarts'
app.provide('$echarts', echarts)
使用 test.vue
import { inject } from 'vue'
const echarts = inject('$echarts')
<div ref="testRef"></div>
import { ref } from 'vue'
testRef = ref(null)
在 onMounted
获取 testRef
才有值
- 子组件
import { useContext } from 'vue'
const { expose } = useContext()
const screenAdapter = () => {
//...
}
// 暴露出去
expose({
screenAdapter
})
- 父组件
<Child ref="childRef"></Child>
<script setup>
import { ref } from 'vue'
// 这里必须这样写 不能写 let childRef = ref(null)
ref: childRef = null
// 调用方法
childRef.screenAdapter()
</script>