使用angular路由器时的StaticInjectorError和NullInjectorError

时间:2019-06-05 03:43:46

标签: angular router angular-router

这是我的app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { DslistingsComponent } from './dslistings/dslistings.component';
import {MatCardModule} from '@angular/material/card';
import {MatGridListModule} from '@angular/material/grid-list';
import {MatExpansionModule} from '@angular/material/expansion';
import {MatButtonModule} from '@angular/material/button';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatChipsModule} from '@angular/material/chips';
import {MatDividerModule} from '@angular/material/divider';
import {MatListModule} from '@angular/material/list';
import { RouterModule, Router } from '@angular/Router';


@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent,
    DslistingsComponent
  ],
  imports: [
    BrowserModule,
    MatCardModule,
    MatGridListModule, 
    MatExpansionModule,
    MatButtonModule,
    BrowserAnimationsModule,
    MatChipsModule,
    MatDividerModule,
    MatListModule,
    RouterModule.forRoot([{path:"", component: DslistingsComponent}])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是我的navbar.component.ts

import { Component, OnInit } from '@angular/core';
import * as cloneDeep from 'lodash/cloneDeep';
import { Location } from '@angular/common';

import { Router } from '@angular/router';
import * as customUrl from '../CustomUrlSerializer';

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})


export class NavbarComponent implements OnInit {


  constructor(private router: Router) {

  }


  ngOnInit() {}

}

为什么即使在app.module文件中导入RouterModule,然后在navbar.component.ts文件中声明了私有路由器,我仍会收到错误消息? enter image description here

html已经具有,我尝试删除私有路由器:从component.ts文件中删除路由器,并且在我尝试将路由器添加到构造函数中时,页面加载没有错误,

StaticInjectorError(AppModule)[NavbarComponent -> Router]: 
  StaticInjectorError(Platform: core)[NavbarComponent -> Router]: 
    NullInjectorError: No provider for Router!
NullInjectorError: StaticInjectorError(AppModule)[NavbarComponent -> Router]: 
  StaticInjectorError(Platform: core)[NavbarComponent -> Router]: 
    NullInjectorError: No provider for Router!

0 个答案:

没有答案