JavaScript/vue.js/getCurrentInstance:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“== 标题文本 == == 获取全局注册组件 == <syntaxhighlight lang="jsx"> <template> <EchartsLinePanel v-if="options" :layerOptions="options" @update:layerOptions="handleUpdateOptions" /> </template> <script lang="ts" setup> import { defineAsyncComponent, getCurrentInstance, type ComponentInternalInstance } from 'vue' const { appContext } = <ComponentInternalInstance>getCurrentInstance() const EchartsLinePanel = defineAsyncComponent({…”
 
Kangkk​(留言 | 贡献)
 
(未显示同一用户的8个中间版本)
第1行: 第1行:
== 标题文本 ==
== 标题文本 ==
== 获取全局注册组件 ==
== 获取全局注册组件 ==
<syntaxhighlight lang="jsx">
最简单的方法是借助 window
<syntaxhighlight lang="javascript">
window.app = app;</syntaxhighlight>
 
以及
<syntaxhighlight lang="javascript">
document.querySelector('#app').__vue_app__</syntaxhighlight>
 
以上两种方法是可以在在外部的 js 文件中获取 vue 的 createApp 返回实例,通过实例('''app.component(name)''')即可获取全局注册组件。
 
以下两种常规方法反而只能在 vue 文件中获取 vue 的实例。
 
<syntaxhighlight lang="html">
<template>
<template>
   <EchartsLinePanel
   <EchartsLinePanel
第23行: 第35行:
</script>
</script>
</syntaxhighlight>
</syntaxhighlight>
以及
{{hc|head=./src/main.js|output=<syntaxhighlight lang="javascript">
import { createApp } from 'vue'
import { store } from './store'
const app = createApp(App)
app.provide('appMounted', app )
</syntaxhighlight>}}
借助 provide 注册顶层变量的另外一个思路是包装成一个插件
<syntaxhighlight lang="javascript">
// define a plugin
const key = "__CURRENT_APP__"
export const ProvideAppPlugin = {
    install(app, options) {
        app.provide(key, app)
    }
}
export function useCurrentApp() {
    return inject(key)
}
// when create app use the plugin
createApp().use(ProvideAppPlugin)
// get app instance in Component.vue
const app = useCurrentApp()
return () => h(app.version)</syntaxhighlight>
{{Unknown|https://stackoverflow.com/questions/75446100/vue-3-get-current-application-instance}}
所以在这里补充一种借助事件总线的通用方法获取 vue 实例,某种程度上的说法是这种才是正解。
{{hc|head=./src/events/bus.js|output=<syntaxhighlight lang="javascript">
import mitt from 'mitt'
export const emitter = mitt()
export const useBus = () => ({ bus: emitter })
</syntaxhighlight>}}
{{hc|head=./src/main.js|output=<syntaxhighlight lang="javascript">
import { createApp } from 'vue'
import { emitter } from './events/bus'
const app = createApp(App)
// ###
// 全局可用方法
// declare module '@vue/runtime-core' {
//  export interface ComponentCustomProperties {
//    emitter: typeof emitter
//  }
// }
app.config.globalProperties.$emitter = emitter
emitter.emit('appMounted', app)
export default app.mount('#app')
</syntaxhighlight>}}
在其他 js 文件中使用,借助 js 文件的上下文
{{hc|head=./src/store/action.js|output=<syntaxhighlight lang="javascript">
import { useBus } from '@/events/bus.js'
let vueApp = null
useBus().bus.on('appMounted', (vm) => {
  console.log(vm)
  vueApp = vm
})
export default {
  async registryComponent({ commit, dispatch }, name) {
    try {
      console.log('vueApp', vueApp)
// ...
</syntaxhighlight>}}
除此之外还有 vuex 等方法
{{hc|head=./src/main.js|output=<syntaxhighlight lang="javascript">
import { createApp } from 'vue'
import { store } from './store'
const app = createApp(App)
store.commit('appMounted', app )
// useage: store.state.appMounted
</syntaxhighlight>}}