JavaScript/vue.js/getCurrentInstance:修订间差异
来自康健生活
| 第23行: | 第23行: | ||
</script> | </script> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
另外一个思路 | |||
<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}} | |||