Long Dao
Long Dao

Reputation: 1371

Add parameters to constructor in Ionic 4

I am new to Ionic. I got a location page, and under location/location.page.ts file, there is an empty constructor generated automatically as bellow:

constructor() { }

The page runs fine with it. However, if I add any parameter to the constructor, the page cannot loaded.

constructor(private httpProvider: HttpProvider, public geolocation: Geolocation) { }

Do I have to do anything else when add parameters to constructor? I am working on an Ionic 4 project. I also added it in the appmodule.ts file. Am I still missing anything?

enter image description here

enter image description here

Thanks.

Upvotes: 0

Views: 1980

Answers (2)

manish kumar
manish kumar

Reputation: 4702

Also for other services

use { providedIn: 'root' } like this in the services.

@Injectable(
    { providedIn: 'root' }
)

Upvotes: 0

Mehdi
Mehdi

Reputation: 2398

You need to import the HttpModule in your app.modules.ts like so :

import { HttpModule } from '@angular/http';

Then add add it to your imports :

imports: [ 
    /// 
    HttpModule,
    ///
   ]

In the same file you need to declare your Gelocation provider :

import { Geolocation } from '@ionic-native/geolocation';

This time add Geolocation to the providers array.

Now in your LocationPage, your constructor needs to look like:

constructor(private http: Http, private location: Geolocation){
   ///
}

provided that you have installed the appropriate npm packages as explained here

Upvotes: 1

Related Questions