使用angular-cli在angular2中获取http:// localhost:4200 / null 404(未找到)

Mingyu Jeon :

我正在使用angular-cli处理angular2项目。今天,我将以下版本更新为2.4.1。

"@angular/common": "~2.4.1",
"@angular/compiler": "~2.4.1",
"@angular/compiler-cli": "^2.4.1",
"@angular/core": "~2.4.1",
"@angular/forms": "~2.4.1",
"@angular/http": "~2.4.1",
"@angular/platform-browser": "~2.4.1",
"@angular/platform-browser-dynamic": "~2.4.1",
"@angular/router": "~3.4.1",
"angular-cli": "^1.0.0-beta.24"

当我使用2.0.0时,它不会出错,但是现在,它会出错GET http://localhost:4200/null 404 (Not Found)

即使出现错误,它也可以正常运行。但是,我想知道为什么会发生,并解决此错误。如果有人知道这一点,请告诉我。谢谢 :)

Packages.json

{
  ...
  
  "private": true,
  "dependencies": {
    "@angular/common": "~2.4.1",
    "@angular/compiler": "~2.4.1",
    "@angular/compiler-cli": "^2.4.1",
    "@angular/core": "~2.4.1",
    "@angular/forms": "~2.4.1",
    "@angular/http": "~2.4.1",
    "@angular/material": "^2.0.0-beta.0",
    "@angular/platform-browser": "~2.4.1",
    "@angular/platform-browser-dynamic": "~2.4.1",
    "@angular/router": "~3.4.1",
    "@types/moment-timezone": "^0.2.33",
    "angular-cli": "^1.0.0-beta.24",
    "angular2-google-maps": "^0.17.0",
    "bootstrap": "^3.3.7",
    "bourbon": "^4.2.7",
    "core-js": "^2.4.1",
    "es6-promise": "^4.0.5",
    "font-awesome": "^4.7.0",
    "hammerjs": "^2.0.8",
    "moment": "^2.17.1",
    "moment-timezone": "^0.5.10",
    "node-sass": "^3.13.0",
    "primeng": "^1.1.0",
    "pubnub-angular2": "^1.0.0-beta.6",
    "quill": "^1.1.8",
    "rxjs": "5.0.1",
    "ts-helpers": "^1.1.1",
    "typescript": "^2.0.10",
    "typings": "^2.1.0",
    "zone.js": "^0.7.4"
  },
  "devDependencies": {
    "@types/hammerjs": "^2.0.33",
    "@types/jasmine": "^2.2.30",
    "@types/moment": "^2.13.0",
    "@types/moment-timezone": "^0.2.33",
    "@types/node": "^6.0.42",
    "angular-cli": "^1.0.0-beta.24",
    "bootstrap-sass": "^3.3.7",
    "codelyzer": "~0.0.26",
    "jasmine-core": "2.4.1",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "4.0.9",
    "ts-loader": "^1.3.3",
    "ts-node": "1.2.1",
    "tslint": "4.2.0",
    "typescript": "2.0.2"
  }
}

angular-cli.json

{
  "project": {
    "version": "1.0.0-beta.24",
    "name": "five-delivery-admin"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": ["assets"],
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "styles.scss",
        "../node_modules/hammerjs/hammer.min.js",
        "../node_modules/primeng/resources/themes/omega/theme.css",
        "../node_modules/primeng/resources/primeng.min.css",
        "../node_modules/font-awesome/css/font-awesome.min.css",
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "../node_modules/quill/dist/quill.core.css",
        "../node_modules/quill/dist/quill.snow.css",
        "../node_modules/quill/dist/quill.bubble.css"
      ],
      "scripts": [
        "../node_modules/quill/dist/quill.min.js",
        "../node_modules/hammerjs/hammer.min.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "prefixInterfaces": false
  }
}

tsconfig.json

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]
  }
}

HauntedSmores:

所以我只是花了很多时间才能弄清楚这一点,但是我做到了。

在执行更新后,这件事就发生在我身上,但与此无关。

绝对是从对具有的变量的引用中得出的,并且很可能是图像src属性。至少对我来说,这是我在应用程序各处使用的服务器中的值,而imgsrc是导致此问题的唯一原因,这很有意义。

我有类似的东西:

<img [src]="myData?.imageUrl">

我以为?安全运算符可以确保我不会出现空错误,通常这是这样做的,但是当您绑定到src属性时,浏览器会抛出一个错误,而不是编译器。编译器由于安全的运算符而使您可以将null绑定到它,但是由于src它是参考URL,因此浏览器仍在寻找该URL,并且控制台由于找不到URL而引发错误。relative/app/path/null

我找到了两种解决方法:

  1. 使用ngIf :(<img *ngIf="myData" [src]="myData.imageUrl"/>如果没有数据,整个映像及其src属性甚至都不会加载,从而完全避免了此问题)
  2. 使用插值:(<img src="{{ myData?.imageUrl }}"/>在此解决方案中,请注意,您仍然需要安全的运算符,因为编译器将按预期方式否则引发错误,但现在如果该值为null,则src=""代替null,这是有效的HTML,并且不会引发引用错误)

希望这可以帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

http:// localhost / undefined 404(未找到)

POST http:// localhost:3000/404(未找到)

Angular 2 + angular-in-memory-web-api区域GET http:// localhost:3000 / traceur 404(未找到)

GET http:// localhost:4200 / node_modules / zone.js / dist / zone.js 404(未找到)

在angular4中获取http:// localhost:3000 / node_modules / rxjs / 404(未找到)

ionic3 GET http:// localhost:8100 / null 404(未找到)

Angular2和节点js中的POST http:// localhost:4200 / addTask 404(未找到),但值已在控制台中打印

使用Angular-Apollo graphql客户端获取404未找到

GET http:// localhost:4200 / resource 404(未找到)

POST http:// localhost:4200 / contact / send 404(未找到)

AspNetCore 2.2 Angular SignalR协商始终返回404未找到

重新加载Angular子组件将用户带到localhost:4200

Angular 8:POST http:// localhost:4200 / assets / data / students.json 404(未找到)

(404未找到)正在加载http:// localhost:4200 / angular2-datatable / datatable

Angular2获取错误404未找到

Angular2 加载资源失败:服务器响应状态为 404(未找到)

GET http://localhost:4200/src/app/ficheros/nacionalidades.json 404(未找到)

在 Angular 5 中将 localhost:4200 更改为 mysite:4200

GET http://localhost:5000/error 404 (未找到)

连接被拒绝:localhost:4200 favicon.ico 502 in angular 7

未找到 Http 404

PUT http://localhost:3000/events/undefined 404(未找到)

在 Angular 中获取 http://localhost:4200/[object%20Blob] 404(未找到)

POST http://localhost:55703/ValidStep1 404(未找到)

“http://localhost:49540/api/create/ 的 HTTP 失败响应:404 未找到”Angular 11 POST 请求

GET http://localhost:3000/:/localhost:4000 404(未找到)

GET http://localhost:3000/hello 404(未找到)

GET http://localhost:5000/auth/me 404(未找到)

使用 Pug 表达:GET http://localhost:3004/contact 404(未找到)