Angular 如何在';ng构建';

Angular 如何在';ng构建';,angular,angular-cli,Angular,Angular Cli,我想将angular cli与asp.net core一起使用,我需要知道如何更改dist文件夹的路径 注意:正确答案如下。这已经不起作用了 在项目中创建名为.ember cli的文件,并在其中包含以下内容: { "output-path": "./location/to/your/dist/" } 您也可以使用CLI,如: ng build -prod --output-path=production # or ng serve --output-path=devroot 另一个

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

注意:正确答案如下。这已经不起作用了

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

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

您也可以使用CLI,如:

ng build -prod --output-path=production

# or

ng serve --output-path=devroot

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

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

或者无论您的dist dir的路径是什么。

Angular CLI现在使用环境文件来完成此操作

首先,在
angularcli.json

比如:

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

export const environment = {
  production: true,
  "output-path": "./whatever/dist/"
};
现在,当您运行时:

ng build --prod

它将输出到
/which/dist/
文件夹。

更流行的方法是更新
.angular cli.json
中的
outDir
属性

ng build
命令参数
--output path
(简称
-op
)也仍然受支持,如果需要多个值,可以将它们作为npm脚本保存在
包.json

注意:像上面所说的那样,
.angular cli.json
属性没有被调用
输出路径。这只是
ng build
参数

如上所述,它被称为
outDir
,是
apps
属性下的一个属性

附笔。 (2017年12月)


添加此答案一年后,有人添加了一个基本相同信息的新答案,最初的海报将接受的答案改为迟了一年的答案,在第一行中包含相同的信息。

对我来说唯一有效的方法是在
angular cli.json
src/tsconfig.json
中更改
outDir

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

以下是最重要的几行

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

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

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

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

对于Angular 6+来说,情况发生了一些变化

定义ng build生成应用程序文件的位置 Cli设置现在在工作区根目录中的angular.json(替换为.angular Cli.json)中完成。default angular.json中的输出路径应如下所示(删除不相关的行):

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

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

美国

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

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

在angular.json中:

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

如果您不知道在构建时应用程序将托管在何处,您可以在generated index.html中更改base标记

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

entrypoint.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
if[-n“${BASE_PATH}”]
然后
文件=($(find.-name“index.html”))
cp-n“${files[0]}”“${files[0]}.org”
cp“${files[0]}.org”“${files[0]}”
sed-i“s***g”“${files[0]}”
fi

对于我使用的github页面

ng build --prod --base-href "https://<username>.github.io/<RepoName>/" --output-path=docs
ng build--prod--base href”https://.github.io//“--输出路径=文档
这就是将输出复制到docs文件夹中的内容:
--output path=docs

注意:角度6及以上
对于具有
angular.json
(而不是
angular cli.json
)的读卡器,正确的键是outputPath。我猜angular 6中的angular配置更改为
angular.json
,因此如果您使用的是版本6或更高版本,则很可能会有一个
angular.json
文件

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

示例
angular.json

{
“$schema”:“/node_modules/@angular/cli/lib/config/schema.json”,
“版本”:1,
“项目”:{
“角度应用程序”:{
“项目类型”:“应用程序”,
[...]
“建筑师”:{
“构建”:{
“生成器”:“@angular devkit/build angular:browser”,
“选择”:{
“输出路径”:“距离/角度应用程序”,
“索引”:“src/index.html”,
“main”:“src/main.ts”,
[...]

我找不到这方面的任何官方文件(如我所料,未包含在中),可能有人可以在此链接官方资源。

是否可以覆盖angular-cli.json中的输出目录?此功能似乎与此请求配合使用:看起来是最佳答案,但angular cli仍将我的文件发布在dist文件夹中。版本:
angular cli:1.0.0-beta.21
From angular cli:outDir(字符串):生成结果的输出目录。默认为dist/@hbthanki,这是cli开关,而不是环境文件json属性。可以将index.html放在./location/toYour/dist中,并将javascript捆绑包放在子文件夹中,例如../location/toYour/dist/bundle。任何人都知道进一步自定义outDir的更新吗?css/ts/在单独的路径中s@fidev看看资产的“output”键,至少对css文件有效。这个答案在Angular 6+中不再有效。下面更好。是否可以将index.html放在./location/toYour/di中
ng build --base-href=/my-folder/
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
ng build --prod --base-href "https://<username>.github.io/<RepoName>/" --output-path=docs