Base-href / deploy-url参数无法按预期工作:构建后不会更改路径

时间:2018-08-14 12:15:03

标签: angular angular-cli

我使用Angular 6

index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My App</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>

<script src="./config.js"></script>
</body>
</html>

此外,一个组件的模板还包含img标签:

<img src="assets/spinner.gif">

如果我使用base-href / deploy-url参数构建它,除了config.js和asset / spinner.gif的路径,所有其他都可以正常运行(base href,所有css文件等等)-angular-cli保持这些路径没有变化。 该如何解决?

PS。我尝试使用"assets/spinner.gif""/assets/spinner.gif""./assets/spinner.gif"-结果相同。

PPS angular.json的片段:

    {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "project_name": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/project_name",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/web.config",
              "src/config.js",
              "src/assets"
            ],

1 个答案:

答案 0 :(得分:1)

使用base-href参数时,务必确保使用的路径以尾随/结束,并且HTML中的路径不以/开头,因此必须以以下结尾:

<base href="/dir/"/>

<img src="assets/spinner.gif"/>

没有结尾的/,将不能正确应用基数。