如何在角度4上设置公共路径

时间:2017-04-19 13:34:27

标签: angular angular-cli

所以我需要配置我的应用程序的公用文件夹,换句话说, 我希望资产指向生成的索引文件上的自己的路径 比如src =" mypublicpath / assets / app.js"

我确定这是一个需要在angular-cli.json配置上设置的值。

这是我的档案

{
  "project": {
    "version": "1.0.0-beta.28.3",
    "name": "ng-app-manager"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "../bundles/ng-app-manager",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "files": "src/**/*.ts",
      "project": "src/tsconfig.json"
    },
    {
      "files": "e2e/**/*.ts",
      "project": "e2e/tsconfig.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "inline": {
      "style": false,
      "template": false
    },
    "spec": {
      "class": false,
      "component": true,
      "directive": true,
      "module": false,
      "pipe": true,
      "service": true
    }
  }
}

我只需要等同于webpacks" publicPath"

1 个答案:

答案 0 :(得分:3)

您可能正在寻找Angular CLI中的deployUrl选项。您的angular-cli.json看起来像这样。

{
  "project": {
    "version": "1.0.0-beta.28.3",
    "name": "ng-app-manager"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "..\/bundles\/ng-app-manager",
      "deployUrl": "http://my.cdn.host.com/assets",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [

      ],
      "environments": {
        "source": "environments\/environment.ts",
        "dev": "environments\/environment.ts",
        "prod": "environments\/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": ".\/protractor.conf.js"
    }
  },
  "lint": [
    {
      "files": "src\/**\/*.ts",
      "project": "src\/tsconfig.json"
    },
    {
      "files": "e2e\/**\/*.ts",
      "project": "e2e\/tsconfig.json"
    }
  ],
  "test": {
    "karma": {
      "config": ".\/karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "inline": {
      "style": false,
      "template": false
    },
    "spec": {
      "class": false,
      "component": true,
      "directive": true,
      "module": false,
      "pipe": true,
      "service": true
    }
  }
}

然后,运行ng build会自动为dist文件夹中的资源引用添加前缀http://my.cdn.host.com/assets。例如,app.js的引用将转换为http://my.cdn.host.com/assets/app.js