加载HomeComponent,Angular 4时出错

时间:2018-07-13 10:00:28

标签: angular typescript routing angular-httpclient

我正在尝试使用“冰与火的API”构建应用。我收到此错误 ERROR Error: "[object Object]"当我尝试重定向到homeComponent时。 这是错误的屏幕截图:

Before redirecting to home

on home

嗯,还有一件事,我认为shell页没有重定向到首页,因为URL不会更改为http://localhost:4200/home

我正在附加我的代码的plunk。请帮忙。 谢谢。

1 个答案:

答案 0 :(得分:1)

最后找出原因:)。我忘了在我的app.module.ts文件中导入并包含HttpClient并导入数组,从而使该应用无法访问它(我认为是)。

`import {BrowserModule} from'@ angular / platform-b​​rowser';     从'@ angular / core'导入{NgModule};

//Router Module for Application level Route
import { RouterModule,Routes } from '@angular/router';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { BookViewComponent } from './book-view/book-view.component';

//import statement for service
import { BookService } from './book.service';
import { BookHttpService } from './book-http.service';

import { HttpClientModule } from '@angular/common/http'; 

//decorators
@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    BookViewComponent,
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot([
      { path: 'home', component: HomeComponent },
      { path: 'book/:isbn', component: BookViewComponent }
    ]),
    HttpClientModule
  ],
  providers: [BookService,BookHttpService],
  bootstrap: [AppComponent]
})

export class AppModule { }

我忘记包含此模块import { HttpClientModule } from '@angular/common/http';

但是第二个错误仍然存​​在。 SyntaxError: unexpected token: '{' scripts.js:1:5 。我不明白为什么这是一个错误。

angular.json文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "series": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/series",
            "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"

            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.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": "series:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "series:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "series: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/**"
            ]
          }
        }
      }
    },
    "series-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "series:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "series:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "series"
}