1

I'm trying to import a component into my injectable service, but it seems that I'm doing something wrong. This happens for other pages in similar situations as well. The current situation seems to happen because the LoginPage that I'm trying to import into SessionService requires SessionService, which smells of an infinite loop somewhere. Any ideas on how I should resolve it other than not including the page component in the injectable SessionService?

My current architecture is built so that I need to redirect to login page if session does not exist. Ionic NavController requires a page component for pushing onto navigation stack. Alternate ways of solving the redirection problem are welcome, but I would also like to understand why this is happening.

Uncaught Error: Can't resolve all parameters for LoginPage: (?, ContentService).

session.service.ts

@Injectable()
export class SessionService {
    constructor(private content: ContentService) {}
}

login.page.ts

@Component({
  templateUrl: './login.page.html'
})
export class LoginPage {
  constructor(public session: SessionService,
              public content: ContentService) {
  }

and for those who requested it, content.service.ts

@Injectable()
export class ContentService {
  constructor(private authHttp: AuthHttp) {
  }
}

app.module.ts

@NgModule({
  declarations: [
    LoginPage
  ],
  entryComponents: [
    LoginPage
  ],
  providers: [
    SessionService,
    ContentService
  ]
})
export class AppModule {
}

I'm using Angular 2.2.1 with Ionic 2.0.0-rc.4.

Lightheaded
  • 644
  • 8
  • 23
  • Did you define `ContentService` like `SessionService`? – Mr_Perfect Dec 22 '16 at 10:51
  • why do you want to import component into service?? – raj Dec 22 '16 at 11:24
  • It isn't obvious from the code above that SessionService is defined as a provider. @Injectable decorator is not enough. It is not clear where and why this *LoginPage that I'm trying to import into SessionService requires SessionService* happens. Please, don't skip the necessary details with `...`. – Estus Flask Dec 22 '16 at 12:23
  • I have not defined it as a provider and never have in my project. Maybe I should, but everything else has worked fine without defining providers in the Component() decorator. Also, I edited the original question to be more precise. Please let me know if I should clarify further. – Lightheaded Dec 22 '16 at 15:00
  • Oh, right. Didn't realise to provide the app module. I don't have anything in the declarations. But the app module isn't out of the ordinary. Adding it to the question. – Lightheaded Dec 23 '16 at 10:50
  • Even i have a similar question. I am using RC 6 with NgModule, platformBrowserDynamic().bootstrapModule. The providers [] is not accessible inside the component constructor – anusree Jan 25 '17 at 05:29

1 Answers1

0

content.service.ts

@Injectable()
export class ContentService {...}

Add this also like SessionService

Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62