typescript - 在构建的 vue3/vite 应用程序中访问当前模块名称
问题描述
在我的 vue3/vite/typescript 应用程序中,我使用import.meta.url
并提取文件名来获取当前模块的名称:
文件“myModule.ts”:
export default {
url: import.meta.url
}
文件'test.ts':
import myModule from "./myModule"
console.log(myModule.url)
这很好用,只要我运行开发服务器(它实际上导入模块),但如果我使用构建版本会失败。原因是,构建版本是使用 rollup.js 创建的,它创建一个文件并且不再真正导入任何内容。
但是在这种情况下我怎么可能得到模块的名称呢?我可以以某种方式继续import.meta
工作还是有其他方法可以实现这一点(除了在url
属性中复制文件名)?
解决方案
Use getCurrentInstance
this has most of the things the Options API this
had
import {
getCurrentInstance
} from 'vue'
export default {
setup() {
const instance = getCurrentInstance();
}
}
And do some digging in this object to find the name of your component!
推荐阅读
- embedded - 任务和优化编译器之间的区别
- c++ - 从 SDL 库中的函数返回的指针
- reactjs - React JSX 如何将父自定义反应组件状态属性值传递给子客户组件?
- r - 如何在 ggplot2 R 折线图中获得插值
- python - Python multiprocess.pool 将从父进程分叉大内存
- python - 每次在文件中找到一个单词时,如何让我的文本编辑器 Python 插件执行?
- python - For 循环中的 to_csv 命令未按预期工作
- reactjs - fireEvent.click() 之后的笑话测试结果(没有酶)
- firebase - 如何在我的数据库中的所有当前文档中添加一个新字段?
- postgresql - postgreSQL kubernetes helm安装——从slave读取