【问题标题】:The Base-href/deploy-url parameters don't work as expected: paths aren't changed after buildingBase-href/deploy-url 参数未按预期工作:构建后路径未更改
【发布时间】:2019-01-21 07:08:27
【问题描述】:

我使用 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 参数构建它,一切正常(基本 href、所有 css 文件等),除了 config.js 和 assets/spinner.gif 的路径 - 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"
            ],

【问题讨论】:

  • 你能添加你的 angular.json 文件吗?具体来说,构建 -> 选项下的部分?

标签: angular angular-cli


【解决方案1】:

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

&lt;base href="/dir/"/&gt;

&lt;img src="assets/spinner.gif"/&gt;

如果没有尾随 /,将无法正确应用基础。

【讨论】:

  • 我添加了angular.json 的片段。并将所有必要的文件复制到输出目录:config.js 和 gif 图像。
  • 看起来是正确的 - 如果您在浏览器中浏览到 /assets/spinner.gif 和 /config.js,您是否真的可以加载文件?
  • 我使用带有虚拟目录的 IIS:http://localhost/dir 可以访问整个应用程序。我可以使用http://localhost/dir/assets/spinner.gif 访问这个GIF。但问题是 angular 试图使用http://localhost/assets/spinner.gif 来访问这张图片。
  • 当您将基数设置为 / 时,我认为这是预期且正确的...如果您在 /dir/ 文件夹中运行它,我认为基数应该是设置为 /dir/。加载网站时检查 Chrome 开发工具的网络标签,看看它试图从哪些确切的 URL 加载。
  • 欢迎@DonTomato :) 这是一个有趣的问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多