vue3 定義使用全局變量的示例詳解
vue3 定義使用全局變量
在main.js
import { createApp } from 'vue'
import App from './App.vue'
// 引入element
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// 引入router
import router from './route/index'
const app = createApp(App)
app.use(ElementPlus)
app.use(router)
// 定義全局變量
app.config.globalProperties.$key= "這是一個全局變量";
app.mount('#app')在頁面中使用
import {getCurrentInstance} from 'vue'
const internalInstance = getCurrentInstance()
// 全局變量
let global=internalInstance.appContext.config.globalProperties;
console.log(global.$key)getCurrentInstance 只能在 setup 或生命周期鉤子中調(diào)用。
Vue3全局變量使用
全局變量(函數(shù)等)可以在任意組件內(nèi)訪問,可以當(dāng)組件間的傳值使用。
main.js
import './assets/main.css'
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App);
app.config.globalProperties.$global_id=10;
app.config.globalProperties.$global_name="test";
app.provide('global_code', "code");
app.config.globalProperties.global_value="value";
app.mount('#app');App.vue
<template>
<main>
<button @click="clicked">全局變量</button>
</main>
</template>
<script>
import {inject, getCurrentInstance } from 'vue'
export default {
setup () {
const { proxy } = getCurrentInstance();
const codec = inject("global_code");
function clicked(params) {
console.log(proxy.$systemId)
console.log("global_id:",proxy.$global_id)
console.log("global_name:",proxy.$global_name);
console.log("global_value:",proxy.global_value);
console.log("codec:", codec);
}
return {clicked}
}
}
</script>
<!-- <script setup>
//setup的實現(xiàn)
import { inject,getCurrentInstance } from 'vue'
const codec = inject("global_code");
const instance = getCurrentInstance();
function clicked()
{
console.log("test");
console.log("global_id:",instance.appContext.config.globalProperties.$global_id)
console.log("global_name:",instance.proxy.$global_name);
console.log("global_value:",instance.proxy.global_value);
console.log("codec:", codec);
}
</script> -->getCurrentInstance在使用的時候需要注意
getCurrentInstance 支持訪問內(nèi)部組件實例,用于高階用法或庫的開發(fā)。
import { getCurrentInstance } from 'vue'
getCurrentInstance 只能在 setup 或生命周期鉤子中調(diào)用。
如需在 setup 或生命周期鉤子外使用,請先在 setup 中調(diào)用 getCurrentInstance() 獲取該實例然后再使用。
組合式 API | Vue.js中文網(wǎng) (zcopy.site)
到此這篇關(guān)于vue3 定義使用全局變量的文章就介紹到這了,更多相關(guān)vue3 定義使用全局變量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3+Element Plus進行圖片加載優(yōu)化全攻略
在Web開發(fā)中,未優(yōu)化的圖片會導(dǎo)致很多問題,本文將為大家介紹一下Vue3如何通過Element Plus進行圖片加載優(yōu)化,希望對大家有所幫助2025-03-03
daisyUI解決TailwindCSS堆砌class問題詳解
這篇文章主要為大家介紹了daisyUI解決TailwindCSS堆砌class問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
Vue3?響應(yīng)式高階用法之customRef()的使用
customRef()是Vue3的高級工具,允許開發(fā)者創(chuàng)建具有復(fù)雜依賴跟蹤和自定義更新邏輯的ref對象,本文詳細介紹了customRef()的使用場景、基本用法、功能詳解以及最佳實踐,包括防抖、異步更新等用例,旨在幫助開發(fā)者更好地理解和使用這一強大功能2024-09-09
使用element-ui,el-row中的el-col數(shù)據(jù)為空頁面布局變亂問題
這篇文章主要介紹了使用element-ui,el-row中的el-col數(shù)據(jù)為空頁面布局變亂問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
vue項目中輪詢狀態(tài)更改方式(鉤子函數(shù))
這篇文章主要介紹了vue項目中輪詢狀態(tài)更改方式(鉤子函數(shù)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

