喷油器在 Angular2/Ionic2 中未按预期工作
Injector not working as expected in Angular2/Ionic2
我定义了一个服务如下:
//storageService.ts
import { Injectable } from '@angular/core';
import {Storage, SqlStorage} from 'ionic-angular';
@Injectable()
export class StorageService {
storage: any;
constructor() {
this.storage = new Storage(SqlStorage);
}
getUser() {
return this.storage.get('user');
}
}
我正在将其注入另一个 Class,如下所示:
Profile.ts
import {StorageService} from '../../services/storageService';
@Page({
templateUrl: 'build/pages/profile/profile.html',
providers: [StorageService]
})
export class ProfilePage {
constructor(private http: Http, storageService: StorageService) {
this.storageService.getUser().then(user => {
this.user = JSON.parse(user);
}).catch(error => {
console.log(error);
});
}
}
但是我一直收到错误消息:
Cannot read property 'getUser' of undefined
因为我在构造函数中注入服务..为什么会出现这个错误?
据我所知,您需要 Ionic 中的静态 parameters
getter,例如:
@Page({
templateUrl: 'build/pages/profile/profile.html',
providers: [StorageService]
})
export class ProfilePage {
static get parameters() {
return [[Http], [StorageService]];
}
constructor(private http: Http, storageService: StorageService) {
this.storageService.getUser().then(user => {
this.user = JSON.parse(user);
}).catch(error => {
console.log(error);
});
}
}
因为您使用的是 TypeScript,所以您需要在构造函数中将提供程序定义为 private
或 public
,这将自动在页面 class 中创建提供程序的实例,例如:
constructor(private http: Http, private storageService: StorageService) {
this.storageService.getUser().then(user => {
this.user = JSON.parse(user);
}).catch(error => {
console.log(error);
});
}
其他对您问题的回复提供的 JavaScript 解决方案对您不起作用。
显然我错误地使用了打字稿。
必须如下声明存储服务:
constructor(private http: Http,private storageService: StorageService) {
即我缺少 private 关键字
我定义了一个服务如下:
//storageService.ts
import { Injectable } from '@angular/core';
import {Storage, SqlStorage} from 'ionic-angular';
@Injectable()
export class StorageService {
storage: any;
constructor() {
this.storage = new Storage(SqlStorage);
}
getUser() {
return this.storage.get('user');
}
}
我正在将其注入另一个 Class,如下所示:
Profile.ts
import {StorageService} from '../../services/storageService';
@Page({
templateUrl: 'build/pages/profile/profile.html',
providers: [StorageService]
})
export class ProfilePage {
constructor(private http: Http, storageService: StorageService) {
this.storageService.getUser().then(user => {
this.user = JSON.parse(user);
}).catch(error => {
console.log(error);
});
}
}
但是我一直收到错误消息:
Cannot read property 'getUser' of undefined
因为我在构造函数中注入服务..为什么会出现这个错误?
据我所知,您需要 Ionic 中的静态 parameters
getter,例如:
@Page({
templateUrl: 'build/pages/profile/profile.html',
providers: [StorageService]
})
export class ProfilePage {
static get parameters() {
return [[Http], [StorageService]];
}
constructor(private http: Http, storageService: StorageService) {
this.storageService.getUser().then(user => {
this.user = JSON.parse(user);
}).catch(error => {
console.log(error);
});
}
}
因为您使用的是 TypeScript,所以您需要在构造函数中将提供程序定义为 private
或 public
,这将自动在页面 class 中创建提供程序的实例,例如:
constructor(private http: Http, private storageService: StorageService) {
this.storageService.getUser().then(user => {
this.user = JSON.parse(user);
}).catch(error => {
console.log(error);
});
}
其他对您问题的回复提供的 JavaScript 解决方案对您不起作用。
显然我错误地使用了打字稿。
必须如下声明存储服务:
constructor(private http: Http,private storageService: StorageService) {
即我缺少 private 关键字