【Vue3】app.config.globalProperties如何创建一个储存全局变量的文件

vue3如何创建一个储存全局变量的文件_lhr__的博客-CSDN博客

vue3如何创建一个储存全局变量的文件
在assets文件夹下面创建文件globalvariable.js

// globalvariable.js
// 这里写一些要使用的全局变量 方法也可以

// globalvariable.js
// 这里写一些要使用的全局变量 方法也可以
const aaa = 1234
const fnc = ()=>{ console.log("我执行了")}
export default{
aaa,
fnc
}


main.js中引入并将文件挂载到VUE的原型上

import globalvariable from "@/asstes/js/globalvariable.js"
app.config.globalProperties.$aaa_GLOBAL = globalvariable 
//aaa_GLOBAL 这个是自己定义的名字
// 在组件中使用时无需引入
this.$aaa_GLOBAL.aaa
this.$aaa_GLOBAL.fnc()
//需要注意的是这个东西在setup中无法使用
//因为setup的this为NULL 没有指向VUE的原型

vue3 setup种使用全局变量 globalProperties

vue3 setup种使用全局变量 globalProperties_正在挂机的博客-CSDN博客 

猜你喜欢

转载自blog.csdn.net/weixin_43343144/article/details/121031900