未在Angular 6中加载本地资产

时间:2019-12-23 11:25:35

标签: javascript angular typescript

我之前曾遇到此问题,但忘记了如何解决此问题。 这里的问题很简单,在我的Angular 6项目中,没有从Assets文件夹加载图像。图像路径名是正确的。

我在另一台PC上运行了此代码,并且图像加载正常。我猜我的环境设置中可能存在一些问题,所以我重新安装了节点js,更新了angular.cli并清理了npm缓存

Error Message

这是我的angular.json-

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "angularApi": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/angularApi",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.png",
              "src/assets",
              "src/web.config"
            ],
            "styles": [
//              "./node_modules/bootstrap/dist/css/bootstrap.min.css",
              "./node_modules/slick-carousel/slick/slick.css",
              "./node_modules/slick-carousel/slick/slick-theme.css",
              "./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css",
              "./node_modules/font-awesome/css/font-awesome.css",
              // "./node_modules/bootstrap-select/dist/css/bootstrap-select.min.css",
              "src/styles.css",
              "./node_modules/ngx-toastr/toastr.css"
            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.min.js",
              "./node_modules/slick-carousel/slick/slick.min.js"
//              "./node_modules/bootstrap/dist/js/bootstrap.min.js"
              // "./node_modules/bootstrap-select/dist/js/bootstrap-select.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": "angularApi:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "angularApi:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "angularApi: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/**"
            ]
          }
        }
      }
    },
    "angularApi-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "angularApi:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "angularApi:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "angularApi"
}

请查看angular.json代码并指出是否有错误。

P.S。对不起,错误。感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

由于您要加载的图像位于资产中的某些文件夹内,因此您需要说出角度来加载并将这些文件夹视为资产。

您可以在angular.json文件中执行以下操作:

"assets": [
   "src/favicon.ico",
   "src/assets",
   "src/assets/*" // Add this line at end.
],

在此行的最后,我们要告诉angular在资产文件夹中构建其他文件夹以视为资产。

希望有帮助。