在ng build期间修改javascript脚本标记的src位置

时间:2017-07-26 13:20:38

标签: angular angular-cli

我的index.html文件post ng build具有以下文件

<script type="text/javascript" src="inline.bundle.js"></script>
<script type="text/javascript" src="polyfills.bundle.js"></script>
<script type="text/javascript" src="styles.bundle.js"></script>
<script type="text/javascript" src="vendor.bundle.js"></script>

如何制作角度cli前置&#39; app&#39;在src文件夹位置,例如src="/app/vendor.bundle.js"

我需要进行上述更改,因为我从文件系统位置加载了dropwizard中的所有静态资源,以&#39; / app /&#39;

开头

下面是我的angular-cli.json供参考

角cli.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "web"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "../node_modules/font-awesome/css/font-awesome.min.css",
        "../node_modules/primeng/resources/themes/ludvig/theme.css",
        "../node_modules/primeng/resources/primeng.min.css",
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}

2 个答案:

答案 0 :(得分:8)

angular-cli.json中有一个deployUrl参数:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "web"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "deployUrl": "/app/",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",

您的导入将成为:

<script type="text/javascript" src="/app/inline.bundle.js"></script>
<script type="text/javascript" src="/app/polyfills.bundle.js"></script>
<script type="text/javascript" src="/app/styles.bundle.js"></script>
<script type="text/javascript" src="/app/vendor.bundle.js"></script>

这就是你想要的?

答案 1 :(得分:-1)

在index.html中,您可以为您的应用程序添加基础,例如

  <base href="/app">

话虽如此,我不确定你是否真的需要它,你是否想要在某个地方举办某个路由问题或任何事情?