【问题标题】:Avoiding relative Paths for Angular projects in the workspace避免工作区中 Angular 项目的相对路径
【发布时间】:2019-08-20 18:38:25
【问题描述】:

首先,我应该说我已经使用以下命令创建了我的 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) 当我想在另一个模块中使用此模型时,出现以下错误?

找不到模块“@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

【问题讨论】:

  • 不确定这是否可行,但我认为您的 tsconfig.app.json 中与 shared 文件夹相对应的 glob 不包含子目录。你能试试@app/shared/**/*吗?

标签: angular


【解决方案1】:

不要更改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/*"
      ]
    }
  }
}

【讨论】:

  • 它就像一个魅力。真的我被卡住了,非常感谢@VahidN
  • 那么我该如何为第二个项目abc-web配置类似的路径?
【解决方案2】:

我建议你像这样改变你的 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"
  ]
}

【讨论】:

  • tsconfig.app.json 怎么样,应该怎么样?
  • 不幸的是,它不起作用。我通过添加项目的结构更新了我的问题,因为我猜您认为工作空间的根目录中有一个名为 src 的文件夹,但没有。谢谢回复
【解决方案3】:

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

这是我的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"
  ]
}

【讨论】:

  • tsconfig.app.json 怎么样,应该怎么样?
  • @AbolfazlR - 更新为 tsconfig.app.json
  • @Andrew Hill - 如果您仔细查看我的问题中的项目结构,工作空间的根目录中没有名为 src 的文件夹。在上面的 tsconfig.json 中,baseUrl 属性的值为“./src”(此文件夹不会退出工作空间的根目录)
猜你喜欢
  • 2017-05-18
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
相关资源
最近更新 更多