2017-05-29 7 views
0

のすべてのパラメータ解決できません:アプリのコンポーネントでは私が共有ユーザーデータプロバイダを作成しようとしていますユーザー

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

チェックタイポので、たとえば、あなたはapp.module.tsファイルに追加することができますバレル ' – Aravind

+0

私はそれが注射可能であるとは思わない、またはそうすべきである。それは単なるデータクラスです。注入する場合は、InjectionTokenを使用します。 – jonrsharpe

+0

@Aravindコンストラクタにはタイプミスがありません。また、私はバレルを使用していません。 – Manos

答えて

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を追加する必要があります。

@NgModule({ 
    declarations: [...], 
    imports: [...], 
    bootstrap: [IonicApp], 
    entryComponents: [...], 
    providers: [AccountService] // <- Here! 
}) 
export class AppModule { } 

次に、あなたのページには、ちょうどそれを注入し、それを使用する:あなた `constructor`、あなたは`使用している中

// 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(); 
    } 

} 
関連する問題