繁体   English   中英

'ng build'后如何更改angular-cli中的dist文件夹路径

[英]how to change the dist-folder path in angular-cli after 'ng build'

我想将 angular-cli 与 asp.net core 一起使用,我需要知道如何更改dist文件夹的路径

更新的方法是更新.angular-cli.jsonoutDir属性。

ng build命令参数--output-path (或简称-op )仍然受支持,如果你想要多个值,这很有用,你可以将它们作为 npm 脚本保存在package.json中。

当心: .angular-cli.json属性不像@cwill747所说的当前接受的答案那样被称为output-path 那只是ng build参数。

如上所述,它被称为outDir ,它位于apps属性下。

.

聚苯乙烯

(2017 年 12 月)

添加此答案 1 年后,有人添加了一个具有基本相同信息的新答案,原始海报将接受的答案更改为此答案第一行中包含相同信息的 1 年延迟答案。

对于 Angular 6+,事情发生了一些变化。

定义 ng build 生成应用程序文件的位置

Cli 设置现在在工作区根目录中的angular.json (替换为 .angular-cli.json)中完成。 默认 angular.json 中的输出路径应如下所示(删除了不相关的行):

{
  "projects": {
    "my-app-name": {
    "architect": {
      "options": {
         "outputPath": "dist/my-app-name",

显然,这将在 WORKSPACE/dist/my-app-name 中生成您的应用程序。 如果您更喜欢另一个目录,请修改 outputPath。

您可以使用命令行参数覆盖输出路径(例如对于 CI 作业):

ng build -op dist/example
ng build --output-path=dist/example

https://github.com/angular/angular-cli/wiki/build

在子目录中托管 angular 应用程序

设置输出路径,将告诉 angular 放置“编译”文件的位置,但是无论您如何更改输出路径,运行应用程序时,angular 仍会假设应用程序托管在网络服务器的文档根目录中。

要使其在子目录中工作,您必须设置基本 href。

在 angular.json 中:

{
  "projects": {
    "my-app-name": {
    "architect": {
      "options": {
         "baseHref": "/my-folder/",

客户端:

ng build --base-href=/my-folder/

如果您不知道在构建时应用程序将托管在哪里,您可以在生成的 index.html 中更改基本标记。

这是我们如何在 docker 容器中执行此操作的示例:

入口点.sh

if [ -n "${BASE_PATH}" ]
then
  files=( $(find . -name "index.html") )
  cp -n "${files[0]}" "${files[0]}.org"
  cp "${files[0]}.org" "${files[0]}"
  sed -i "s*<base href=\"/\">*<base href=\"${BASE_PATH}\">*g" "${files[0]}"
fi

您可以更新 .angular-cli.json 中的输出文件夹:

"outDir": "./location/toYour/dist"

您也可以使用 CLI,例如:

ng build -prod --output-path=production

# or

ng serve --output-path=devroot

唯一对我outDir是在angular-cli.jsonsrc/tsconfig.json

我希望我的 dist 文件夹位于 angular 项目文件夹之外。 如果我也没有更改src/tsconfig.json的设置,Angular CLI 会在我构建项目时抛出警告。

以下是最重要的几行...

// angular-cli.json
{
  ...
  "apps": [
    {
      "outDir": "../dist",
      ...
    }
  ],
  ...
}

还有...

// tsconfig.json
{
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    ...
  }
}

当心:正确答案如下。 这不再有效

在您的项目中创建一个名为.ember-cli的文件,并在其中包含以下内容:

{
   "output-path": "./location/to/your/dist/"
}

对于我使用的 github 页面

ng build --prod --base-href "https://<username>.github.io/<RepoName>/" --output-path=docs

这是将输出复制到 docs 文件夹的内容: --output-path=docs output --output-path=docs

Angular CLI 现在使用环境文件来执行此操作。

首先,在angular-cli.json添加一个environments部分

类似的东西:

{
  "apps": [{
      "environments": {
        "prod": "environments/environment.prod.ts"
      }
    }]
}

然后在环境文件(本例中为environments/environment.prod.ts )中,添加如下内容:

export const environment = {
  production: true,
  "output-path": "./whatever/dist/"
};

现在当你运行时:

ng build --prod

它将输出到./whatever/dist/文件夹。

另一种选择是将 webroot 路径设置为 angular cli dist 文件夹。 在您的 Program.cs 中配置 WebHostBuilder 时只需说

.UseWebRoot(Directory.GetCurrentDirectory() + "\\Frontend\\dist")

或者无论你的 dist 目录的路径是什么。

注意:Angular 6 及以上!


对于使用angular.json (不是angular-cli.json )的读者,正确的关键是outputPath 我猜角度配置在 Angular 6 中更改为angular.json ,所以如果您使用版本 6 或更高版本,您很可能有一个angular.json文件。

要更改输出路径,您必须更改outputPath和构建选项。

示例angular.json

{
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "projects": {
        "angular-app": {
            "projectType": "application",
            [...]
            "architect": {
                "build": {
                    "builder": "@angular-devkit/build-angular:browser",
                    "options": {
                        "outputPath": "dist/angular-app",
                        "index": "src/index.html",
                        "main": "src/main.ts",
                        [...]

我找不到任何官方文档(如我所料,未包含在https://angular.io/guide/workspace-config中),也许有人可以链接有关此的官方资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM