typescript - 如何在打字稿项目中使用带有打字稿代码的git子模块
问题描述
我正在开发一个 VueJS 项目,该项目根据环境变量使用不同的颜色和其他一些东西。这些值包含在我作为子模块添加的另一个存储库中。所以我项目中的结构是:
- 我的项目
- 子模块
- 节点模块
- 上市
- 源代码
为了完成这项工作,我将其包含在vue.config.js
:
module.exports = {
chainWebpack: config => {
config.resolve.alias
.set('@theme', path.resolve('the-submodule/' + process.env.VUE_APP_THEME))
},
...
我像这样使用它,例如plugins/vuetify.ts
:
import Vue from 'vue'
import Vuetify from 'vuetify/lib/framework'
import colors from '@theme/colors'
Vue.use(Vuetify)
export default new Vuetify({
theme: {
themes: {
light: {
...colors
}
}
}
})
或在router/index.ts
:
import texts from '@theme/texts'
...
router.afterEach((to) => {
Vue.nextTick(() => {
document.title = to.meta.title ? `${texts.appName} | ${to.meta.title}` : texts.appName
})
})
虽然这确实可以编译并且可以工作,但在编译过程中我仍然会遇到错误:
ERROR in /Users/sme/projects/aedifion-frontend/src/plugins/vuetify.ts(4,20):
4:20 Cannot find module '@theme/colors' or its corresponding type declarations.
2 | import Vue from 'vue'
3 | import Vuetify from 'vuetify/lib/framework'
> 4 | import colors from '@theme/colors'
| ^
5 |
6 | Vue.use(Vuetify)
7 |
ERROR in /Users/sme/projects/aedifion-frontend/src/router/index.ts(12,19):
12:19 Cannot find module '@theme/texts' or its corresponding type declarations.
> 12 | import texts from '@theme/texts'
| ^
这些是主题中的文件示例:
the-submodule/some-theme/colors.ts
:
import { Colors } from '../types'
export default {
accent: '#288946',
error: '#e60046',
info: '#969196',
primary: '#007982',
secondary: '#96BE0D',
success: '#a0af69',
warning: '#fab450'
} as Colors
the-submodule/some-theme/texts.ts
:
import { Texts } from '../types'
export default {
appName: 'MyApp'
} as Texts
the-submodule/types.ts
:
export interface Colors {
accent: string;
error: string;
info: string;
primary: string;
secondary: string;
success: string;
warning: string;
}
export interface Texts {
appName: string;
}
我还为每种类型或所有类型创建了不同版本的.d.ts
文件。the-submodule
我已经尝试了几乎所有我能找到的东西。我如何摆脱这些错误?
编辑:
当我明确映射路径时,它可以工作tsconfig.json
:
{
"compilerOptions": {
"paths": {
"@/*": [
"src/*"
],
"@theme/*": [
"the-submodule/some-theme/*"
]
},
所以我想问题是:如何在tsconfig-json
不明确添加some-theme
到路径的情况下让路径映射工作?
解决方案
推荐阅读
- sql - 写入查询以迭代就业数据表并执行服务持续时间的总和
- r - 使用 Caret 训练不同的 ML 模型
- python - 为什么我在 Python 中使用 ctypes 从 C 函数中得到错误的地址
- docker - 谷歌云运行容器网络
- reporting-services - 具有多列的 SSRS 分组
- opencart - 顶部菜单中的 Opencart 3.0.2.0 和 Journal 3 主题客户名称
- xamarin.forms - 使用 Xamain.Forms.Android 在初始屏幕中播放视频
- halide - 来自生成器的卤化物外部函数调用
- c# - 无法从特定 blob 容器中删除文件
- excel - 添加按钮并在按下时分配功能