2016-12-13 33 views
0

所以我有這個類在一個單獨的文件中:如何使用導出類的Angular2組件

export class MyModel { 
     votes: number; 
     title: string; 
     link: string; 

     constructor() { 
     this.title = 'Github'; 
     /// 
     } 

     myfunc() 

    /// 
} 

而且我想用它的道具在這個組件,但無法弄清楚如何要做到這一點:

import { Component } from '@angular/core'; 
import { MyModel } from './models.model'; 

@Component({ 
    selector: /// 
    templateUrl: /// 
    styleUrls: /// 
}) 
export class SomeComponent { 
    //declare somehow..? 
} 
+0

你可以聲明其爲'讓MyObj中=新爲MyModel()'裏面你'SomeComponent'; – ranakrunal9

回答

0

可以如下聲明它:

import { Component } from '@angular/core'; 
import { MyModel } from './models.model'; 

@Component({ 
    selector: /// 
    templateUrl: /// 
    styleUrls: /// 
}) 
export class SomeComponent { 

    public objOne: MyModel; 
    public objTwo: MyModel; 

    ngOnInit() { 
    this.objOne = { 
     title : 'First Object' 
    }; 

    // or you can declare it as below 
    this.objTwo = new MyModel(); 
    } 

} 

你可以012頁上的檢查app.component.ts代碼

0

您可以簡單地使用new關鍵字聲明一個對象,然後使用該對象訪問title和其他屬性。

例子:

var obj = new MyModel(); 
console.log(obj.title);