我正在尝试创建一个共享的用户数据提供程序:
import {Injectable} from '@angular/core';
@Injectable()
export class User {
public name: string;
public email: string;
public password: string;
public birthday: string;
public settings: Settings;
public loggedIn: boolean;
constructor({
name = null, email = null, password = null, birthday = null,
settings = new Settings(),loggedIn = false
}: {
name?: string; email?: string; password?: string; birthday?: string;
settings?: Settings; loggedIn?: boolean;
} = {}) {
this.name = name;
this.email = email;
this.password = password;
this.birthday = birthday;
this.settings = settings;
this.loggedIn = loggedIn;
}
/**
* @returns {number} the user's age.
*/
get age(): number {
let age = Math.abs(Date.now() - new Date(this.birthday).getTime());
return Math.floor(age * 3.16887646E-11);
}
}
@Injectable()
export class Settings {
notificationSettings: NotificationSettings;
preferencesSettings: PreferencesSettings;
constructor({
notificationSettings = new NotificationSettings(),
preferencesSettings = new PreferencesSettings()
}: {
notificationSettings?: NotificationSettings;
preferencesSettings?: PreferencesSettings;
} = {}) {
this.notificationSettings = notificationSettings;
this.preferencesSettings = preferencesSettings;
}
}
@Injectable()
export class NotificationSettings {
// TODO
}
@Injectable()
export class PreferencesSettings {
// TODO
}
在应用程序组件中,我声明了提供程序,以便使用户全局可用。
当我尝试构建项目时,出现Can't resolve all parameters for User: (?)
错误。
问题出现是因为User
构造函数中的设置,但我很难在逻辑中找到错误。
答案 0 :(得分:2)
我不确定确切的情况,但您的代码问题是您需要知道一件事是共享服务,另一件事是将在该服务中使用的模型(也可能在某些其他服务中使用)。
模型不应该是可注射的,它们只是类。只有服务应该。因此,在您的情况下,我们可以从创建应用的模型开始,然后我们可以添加AccountService
来保存用户的实例。
我已经更改了用作模型的类的名称,以避免混淆(我认为如果名称以-Model结尾,则更容易知道某些东西是模型)
import { Injectable } from '@angular/core';
export class NotificationSettingsModel {
// TODO
}
export class PreferencesSettingsModel {
// TODO
}
export class SettingsModel {
public notificationSettings: NotificationSettingsModel;
public preferencesSettings: PreferencesSettingsModel;
constructor() {
this.notificationSettings = new NotificationSettingsModel();
this.preferencesSettings = new PreferencesSettingsModel();
}
}
export class UserModel {
public name: string;
public email: string;
public password: string;
public birthday: string;
public settings: SettingsModel;
constructor() {
this.name = null;
this.email = null;
this.password = null;
this.birthday = null;
this.settings = new SettingsModel();
}
/**
* @returns {number} the user's age.
*/
get age(): number {
let age = Math.abs(Date.now() - new Date(this.birthday).getTime());
return Math.floor(age * 3.16887646E-11);
}
}
@Injectable()
export class AccountService {
public currentUser: UserModel = null;
constructor() {}
public isLoggedIn(): boolean {
return this.currentUser !== null;
}
}
在此之后,您需要在主应用模块(或共享模块)中添加AccountService
。例如,您可以将其添加到app.module.ts
文件中:
@NgModule({
declarations: [...],
imports: [...],
bootstrap: [IonicApp],
entryComponents: [...],
providers: [AccountService] // <- Here!
})
export class AppModule { }
然后在你的页面中,只需注入并使用它:
// imports...
@Component({...})
export class HomePage{
constructor(public accountService: AccountService) {}
public someMethod(): void {
// You can access the properties
// this.accountService.currentUser;
// And also the methods:
// this.accountService.isLoggedIn();
}
}