首页 > 解决方案 > 如何使用自定义类型绑定导入相关组件

问题描述

所以我有一个像这样设置的项目文件路径:

app
  - src
    - views
      - mypage.vue
  - tsconfig.json
common
  - types
      - test.d.ts
  - test.js

但是,当我尝试像下面这样(在 mypage.vue 中)导入我的 test.js 文件时,根据我实现导入的方式,我收到以下错误。

尝试1:

进口:

import * as test from '../../../common/test'

测试.d.ts

declare module 'test' { ... }

结果: cannot find declaration file for ../../../common/test

尝试2:

进口:

import * as test from '../../../common/test'

测试.d.ts

declare module '../../../common/test' { ... }

结果: module declaration cannot specify relative module name

尝试 3:

tsconfig.json

"paths": {
    "@common/*": [
        "../common/*"
    ]
}

进口:

import * as test from '@common/test'

测试.d.ts

declare module '@common/test' { ... }

结果:

Failed to compile with 1 error This dependency was not found: @common/test To install it, you can run: npm install --save @common/test

标签: typescript

解决方案


推荐阅读