Include own module; very simple question


#1

Hi there,

sorry for this easy question, but I just don’t get it :sweat:

I modified the base tabs-example.

I created a module DatumService in File “/app/datum.ts”

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

@Injectable()
export class DatumService {

    private aDate_MonthList : Array<string>;
}

Now, I want to include it in a page, so i went to the page file (datelist.ts) and wrote

    import { DatumService } from '../app/datum'; 
    @Component({
      selector: 'page-datelist',
      templateUrl: 'datelist.html'
    })
    export class DatelistPage {

      constructor(public navCtrl: NavController, public navParams: NavParams, public TDatum : DatumService) {}
      public pageTitle: string = "Kalender";
    }

Now, I get

“Typescript Error
Cannot find name ‘DatumService’.”

I also tried ./datum, ./datum.ts, …/app/datum.ts.

What is the right way to include it?

Thank you for your help and sorry for wasting your time :disappointed:

Best regards

Frank


#2

What’s the path to go to datum.ts from datelist.ts?

If it’s the default structure generated by the Ionic CLI the page should be pages/datelist/datelist.ts inside src, so the relative path to the service file should be '../../app/datum'.


#3

you need to add the angular2 to your App-Class. In other cases it does not know, that your app has an service with this name.

in your src folder is somewhere your app module definition:

@NgModule({
  declarations: [
    YourPage,
    YourComponent,
    YourPipe
  ],
  imports: [
    IonicModule.forRoot(PizzaAppComponent, {
      backButtonText: ''
    })
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    YourPage
  ],
  providers: [YourService] // Here is the array of your services
})
export class AppModule {}

Your app only knows the stuff you registered here:

  • Pages, Components, Directives, Pipes are declarations --> used somewhere in the dom
  • Providers --> services, used by other parts of the app
  • entryPoints --> Components/Pages which are part of the routing or which you are injecting programmtically in the dom

#4

Thank you both; I made both mistakes; now it works.
About “…/…/”: Now I feel ashamed even more; I should have seen that.:smirk:
About “add anguluar2 to your App-Class”: Seems like I should work through some more tutorials.:smirk: