无法解析用户的所有参数



我正在尝试创建共享用户数据提供商:

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构造函数中的设置而发生的,但是我很难找到逻辑中的错误。

我不确定确切的情况,但是您的代码问题是您需要知道一件事是共享 service ,而另一件事事物是模型,它将在该服务中使用(也可能在其他服务中(。

模型不应是注射量,它们只是类。只有服务。因此,在您的情况下,我们可以从创建您的应用程序的模型开始,然后我们可以添加一个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();
    }
}

相关内容

  • 没有找到相关文章

最新更新