2016-08-22 184 views
2

IM使用angular2版本2.0.0-rc.5 和我創建一個文件名userdetails.ts角2打字稿@injectable

import {Injectable} from '@angular/core'; 

export interface userdetails{ 
    userid: string, 
    password: string, 
    name: string, 
    sbu: string, 
    division: string, 
    position:string, 
    loggedin:boolean 
} 

@Injectable() 
export class Userdetails implements userdetails { 
    public userid: string = null; 
    public password: string = null; 
    public name: string = null; 
    public sbu: string = null; 
    public division: string = null; 
    public position: string = null; 
    public loggedin: boolean = false; 

    constructor(){ 

    } 
    isLoggedin(){ 
     return this.loggedin; 
    } 
    userSBUdetails(){ 
     return this.sbu; 
    } 
    getUserdetails(){ 
     return {userid: this.userid, 
       password: this.password, 
       name: this.name, 
       sbu: this.sbu, 
       division: this.division, 
       position: this.position, 
       loggedin: this.loggedin}; 
    } 
    resetDetails(){ 
     this.userid = null; 
     this.password = null; 
     this.name = null; 
     this.sbu = null; 
     this.division = null; 
     this.position = null; 
     this.loggedin = false; 
    } 
    setDetails(data){ 
     this.userid = data.userid; 
     this.password = null; 
     this.sbu = data.sbu; 
     this.division = data.division; 
     this.position = data.position; 
     this.loggedin = true; 
    } 

} 

,下一步我將其導入到app.module.ts

import { NgModule }  from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule }  from '@angular/http'; 
import { AppComponent }  from './app.component'; 
import { routing, 
     appRoutingProviders } from './app.routing'; 
     //all child route 
import { DominoMenuRouting } from './domino.menu/domino.menu.routing'; 
import { MAPMenuRouting } from './map.menu/map.routing'; 
     //all component 
     //domino.menu 
import { DominoMenuComponent } from './domino.menu/domino.menu'; 
import { PizzaComponent } from './domino.menu/pizza/pizza'; 
import { ToppingComponent } from './domino.menu/topping/topping'; 
     //map.menu 
import { MAPMenuComponent } from './map.menu/map'; 
import { AdminUserComponent } from './map.menu/admin.user/admin.user'; 
import { DomUserComponent } from './map.menu/domino.user/domino.user'; 

import { LoginComponent } from './login/login'; 
import { MainMenuComponent } from './main.menu/main.menu'; 
import { Httpprovider } from './httpprovider'; 
import { Userdetails } from './userdetails/userdetails'; <<-- 

@NgModule({ 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    routing, 
    HttpModule, 
    DominoMenuRouting, 
    MAPMenuRouting, 
    Userdetails <<-- 
    ], 
    declarations: [ 
    AppComponent, 
    LoginComponent, 
    MainMenuComponent, 
    DominoMenuComponent, 
    PizzaComponent, 
    ToppingComponent, 
    AdminUserComponent, 
    DomUserComponent, 
    MAPMenuComponent 
    ], 
    providers: [ 
    appRoutingProviders, 
    Httpprovider 
    ], 
    bootstrap: [ AppComponent ] 
}) 
export class AppModule { 
} 

但是當IM運行它,我得到一個錯誤說

VM60:18 Error: TypeError: Cannot read property 'type' of null(…) 

,但如果我刪除userdetail s從app.module.ts應用程序工作正常, 它有空語句錯誤嗎?

回答

2

imports用於將其他模塊導入到您的模塊中。您的UserDetails不是模塊,它是提供商。因此,將其從imports中刪除,並將其添加到模塊元數據中的providers。 PS:爲什麼它的價值,這裏的命名約定是非常不規則的。

+0

omg我真傻,謝謝你!在我的命名規則中,我會做得更好 – Rommy