简体   繁体   中英

Angular2 Dependency Injection not working on service

I will let you know that I have Dependency Injection working on all of the of of the other Components and services.

Why would router ever be undefined?

import { Router } from '@angular/router';
import { Injectable } from '@angular/core';


@Injectable()
export class ClientQueryActionListProvider {
    constructor(
        private router: Router) {
    }


    goToCorrespondence(): void{        
        let navToRouteUrl = 'someroutehere';
        this.router.navigateByUrl(navToRouteUrl); //while debugging router is undefined 
    }
}

Have you injected RouterModule in app.module.ts(or in current module) like below:

import { RouterModule } from '@angular/router';

@NgModule({
    imports: [
        RouterModule
    ]
})

If not, then import it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM