在工作区中避免Angular项目的相对路径

时间:2019-08-15 10:58:35

标签: angular

首先,我应该说我使用以下命令创建了Angular工作空间:

ng new angular-apps --create-application=false

tsconfig.json在工作区的根目录中:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

然后使用以下命令在此工作区中创建一个项目:

ng generate application cac-web

这是位于cac-web应用程序根目录中的tsconfig.app.json:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/app",
    "types": [],
    "moduleResolution":"classic",
    "baseUrl": "src",
    "paths": {
      "@app/*": [
        "app/*"
      ],
      "@app/core/*": [
        "app/core/*"
      ],
      "@app/shared/*": [
        "app/shared/*"
      ],
      "@env/*": [
        "environments/*"
      ]
    }
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "src/test.ts",
    "src/**/*.spec.ts"
  ]
}

在cac-web应用程序中,我创建了共享模块,然后(模型文件夹/student.model.ts) 当我想在另一个模块中使用此模型时,出现以下错误?

enter image description here

找不到模块'@ app / shared / models / student.model'

编辑:

这是我的项目的结构:

|node_modules
|projects--------------cac-web|e2e-----------------|src
|                             |                    |protractor.conf.js
|                             |                    |tsconfig.json
|                             |src-----------------|app-----------------|client(Module)-----------------|client-list(component)
|                             |                    |                    |shared(Module)-----------------|models(student.model.ts)
|                             |                    |                    |                               |shared.module.ts
|                             |                    |                    |app-routing.module.ts
|                             |                    |                    |app.component.html
|                             |                    |                    |app.component.ts
|                             |                    |                    |app.module.ts
|                             |                    |assets
|                             |                    |environments
|                             |                    |index.html
|                             |                    |main.ts
|                             |                    |polyfills.ts
|                             |                    |styles.css
|                             |                    |test.ts
|                             |browserslist
|                             |karma.conf.js
|                             |tsconfig.app.json
|                             |tsconfig.spec.json
|                             |tslint.json
|.editorconfig
|.gitignore
|angular.json
|package-lock.json
|package.json
|README.md
|tsconfig.json
|tslint.json

3 个答案:

答案 0 :(得分:2)

请勿更改tsconfig.app.json文件。

对于每个新项目,以这种方式修改主tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@app/*": [
        "projects/cac-web/src/app/*"
      ],
      "@app/core/*": [
        "projects/cac-web/src/app/core/*"
      ],
      "@app/shared/*": [
        "projects/cac-web/src/app/shared/*"
      ],
      "@env/*": [
        "projects/cac-web/src/environments/*"
      ]
    }
  }
}

答案 1 :(得分:0)

我建议您这样更改tsconfig.json

注意:tsconfig.json必须与src文件夹处于同一级别

"baseUrl": "./",
"moduleResolution": "node",
"paths": {
      "@app/*": [
        "src/app/*"
      ],
      "@app/core/*": [
        "src/app/core/*"
      ],
      "@app/shared/*": [
        "src/app/shared/*"
      ],
      "@env/*": [
        "src/environments/*"
      ]
    }

还可以在更改tsconfig.json后重新启动您的angular cli服务器

tsconfig.app.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": []
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "src/test.ts",
    "src/**/*.spec.ts"
  ]
}

答案 2 :(得分:0)

此功能目前正在我正在生产中运行的应用程序之一上运行。

这是我的tsconfig.json的样子:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "./src",
    "paths": {
      "@app/*": ["app/*"],
      "@guards/*": ["app/guards/*"],
      "@interceptors/*": ["app/interceptors/*"],
      "@modules/*": ["app/modules/*"],
      "@utilities/*": ["app/utilities/*"]
    },
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "dom",
      "es2016",
      "es2017.object"
    ]
  },
  "angularCompilerOptions": {
    "preserveWhitespaces": false
  }
}

这是我的tsconfig.app.json的样子:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "paths": {
      "@app/*": ["app/*"],
      "@guards/*": ["app/guards/*"],
      "@interceptors/*": ["app/interceptors/*"],
      "@modules/*": ["app/modules/*"],
      "@utilities/*": ["app/utilities/*"]
    },
    "module": "es2015",
    "types": [],
    "target": "es5",
    "allowSyntheticDefaultImports": true,
    "lib": [
      "dom",
      "es2016",
      "es2017.object"
    ]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}