【问题标题】:Angular 6 assets folder is not getting rendered on browserAngular 6资产文件夹未在浏览器上呈现
【发布时间】:2023-03-15 05:56:01
【问题描述】:

我正在学习角度,我正在尝试将 assets 文件夹中的本地图像加载到 main.component.html 中,并且我已经正确设置了路径,同时我在 alt+单击它打开图像的路径上。但是图片没有加载到浏览器上。

我得到的错误

在 chrome 开发者工具的源标签中,没有 assets文件夹包含

这是我的 Angular cli 版本

我试过这个https://github.com/angular/angular-cli/issues/5268 但没用,请有人帮忙 这是我的 angular.json 文件

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "game-of-thrones": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/game-of-thrones",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css",
              "./node_modules/bootstrap/dist/css/bootstrap.min.css",
              "./node_modules/font-awesome/css/font-awesome.css"
            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.min.js",
              "./node_modules/bootstrap/dist/js/bootstrap.min.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "game-of-thrones:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "game-of-thrones:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "game-of-thrones:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "game-of-thrones-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "game-of-thrones:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "game-of-thrones"
}

【问题讨论】:

  • 你能添加你的 angular.json 吗?
  • 你能准确显示哪个 url 给出了 404 错误吗?修改 assets 文件夹后是否重启了 ng serve?
  • @user184994 我添加了angular.json文件看看,
  • @David 这是我的文件夹结构的链接以及我在代码中使用的 img 标签 [link] (imgur.com/a/fpJQgL3),我在修改 assets 文件夹后重新启动了 ng serve,但似乎没有任何效果。
  • 您的路径 /../../assets/books.jpg 看起来不正确。它应该只是 /assets/books.jpg 如果不是,则显示 rreal, final 来自 chrome 的网络选项卡的 URL 失败

标签: angular http-status-code-404 assets file-not-found


【解决方案1】:

通常你需要像这样在你的 index.html 中添加基本 href 前缀:

base href="/game-of-thrones/"

为了在正确的文件夹中查找资产。

【讨论】:

    【解决方案2】:

    我也有同样的问题

    GET http://localhost:4200/assets/img/img.png 404 (Not Found)
    

    但我通过删除项目路径中的所有特殊字符('('、'@'、'$' 等)来修复它。 例如,如果您有一个类似的项目路径

    C:\angular-projects(1)\angularapp
    

    然后改成

    C:\angularprojects1\angularapp
    

    【讨论】:

    • @Satineder Bajwa 很久以前就解决了这个问题,这是我之前遵循的,无论如何感谢您抽出宝贵时间帮助我
    • @AvinashDv 这是一个互相帮助的社区,如果您已经找到答案,那么您应该发布自己问题的答案以帮助他人。
    • @Satineder Bajwa 我发布了那个,我在第二天的问题的 cmets 中找到了答案,还给出了一个链接,在那里我找到了问题的解决方案,我应该在答案部分发布现在,谢谢你
    【解决方案3】:

    通过删除应用程序文件夹位置的所有特殊字符、符号找到上述问题的解决方案,看看这个Github issue

    比如你的项目在E盘,

    更改此位置

    E:\Data\project - 1\Angular(learning)\app
    

    E:\Data\project1\Angularlearning\app
    

    然后,您将能够在 chrome 开发者工具的源选项卡下看到加载到浏览器的资产文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多