Vue3全局屬性app.config.globalProperties的實(shí)現(xiàn)
一、概念
一個(gè)用于注冊(cè)能夠被應(yīng)用內(nèi)所有組件實(shí)例訪問到的全局屬性的對(duì)象。點(diǎn)擊【前往】訪問官網(wǎng)

二、實(shí)踐
2.1、定義
在main.ts文件中設(shè)置app.config.globalPropertie
import {createApp} from 'vue'
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import Pagination from '@/components/Pagination/index.vue';
const app = createApp(App);
//全局方法
app.config.globalProperties.$type = '類型';
app.component('Pagination', Pagination)
app.use(router);
app.use(ElementPlus);
app.mount('#app');
2.2、使用
在Vue文件中使用getCurrentInstance(),通過proxy.$type就可以調(diào)用上面定義的方法
<template>
<el-input v-model="proxy.$type" />
<template>
<script setup>
import { getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
console.log(proxy.$type)
</script>
三、最后
到此這篇關(guān)于Vue3全局屬性app.config.globalProperties的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue3 app.config.globalProperties內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的全局調(diào)用彈窗案例
這篇文章主要介紹了vue 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的全局調(diào)用彈窗案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue2.* element tabs tab-pane 動(dòng)態(tài)加載組件操作
這篇文章主要介紹了vue2.* element tabs tab-pane 動(dòng)態(tài)加載組件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue組件props不同數(shù)據(jù)類型傳參的默認(rèn)值問題
這篇文章主要介紹了vue組件props不同數(shù)據(jù)類型傳參的默認(rèn)值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue中定義src在img標(biāo)簽使用時(shí)加載不出來的解決
這篇文章主要介紹了Vue中定義src在img標(biāo)簽使用時(shí)加載不出來的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue中for循環(huán)更改數(shù)據(jù)的實(shí)例代碼(數(shù)據(jù)變化但頁(yè)面數(shù)據(jù)未變)
這篇文章主要介紹了vue中for循環(huán)更改數(shù)據(jù)的實(shí)例代碼(數(shù)據(jù)變化但頁(yè)面數(shù)據(jù)未變)的相關(guān)資料,需要的朋友可以參考下2017-09-09

