简体   繁体   中英

Understanding Angular2 Dependency Injection in modules

I have written a feature module , and below is the code

app.ticket.service.ts , this is my service which i would like to inject

'use strict';    
import { Injectable } from '@angular/core' 

Injectable()
export class AppTicketService {    

    SaveTicket(): string {
return "service saved ticket";
    }       

}

app.tickets.module.ts , this is my feature module

'use strict';
import { NgModule } from '@angular/core';
import { SharedModule } from '../shared/app.shared.module';
import { AppCreatTicketComponent } from './app.create-ticket.component';
//-- import services
import { AppTicketService } from './app.ticket.service';

//-- import routing
import { ticketRouting } from './app.ticket.routing';
  @NgModule({
    declarations:[ AppCreatTicketComponent],
     imports:[ SharedModule,
               ticketRouting],
     exports:[],
     providers:[AppTicketService]
 })

 export class TicketsModule { }   

app.create-ticket.component.ts , this is my component , which belongs to feature module

 'use strict';
 import { Component , OnInit , Inject} from '@angular/core';    

 //-- import service
 import { AppTicketService } from './app.ticket.service';

 @Component({
 templateUrl: '/tickets/create'
 })

 export class AppCreatTicketComponent implements OnInit{

 ngOnInit(){}

 constructor(public service1 :AppTicketService) {
    let test = this.service1.SaveTicket();      
 }

 }

At runtime I am getting:

"Can't resolve all parameters for AppCreatTicketComponent:" error in browser console

What needs to be modified to resolve this issue?

To fix this issue I had to modify constructor paramter in app.create-ticket.component.ts file

Original

constructor(public service1 :AppTicketService)

Modified

constructor(@Inject(AppTicketService) private service1: AppTicketService)

You can find reason for this here

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