怎么摆脱import,直接使用一个全局变量名 #11324
Unanswered
clh007
asked this question in
Help/Questions
Replies: 2 comments 1 reply
-
Beta Was this translation helpful? Give feedback.
1 reply
-
答案当然是 https://github.com/unplugin/unplugin-auto-import 运行时报错的问题,可以看看你的 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
在utils工具类里面有common和constant,如下图,分别放公共方法和变量





想在组件中使用他们如下,以common为例
主动导入:此时common可以在该文件的ts上下文使用,但是每个vue都要写一个import,很烦,能不能不导入,直接把common做成全局的common
全局1:app.config.globalProperties
需要声明这两个属性
但是这个方法只能在模板中直接使用,在<script setup lang="ts">标签下需要另外导入当前组件的实例,所以也放弃了
全局2:app.provide
需要inject导入,还不如直接import
全局3:


这样编辑器会提示common,但是运行时会提示common为
not defined
显然最后一种方法很不错,但是运行时报错我不会解决
我判断是global只是给编辑器加了语法提示,但是不存在运行时定义,各位该怎么解决这个问题,或者有更好的全局使用common的方法也可,需要能直接使用common的那种
Beta Was this translation helpful? Give feedback.
All reactions