[英]How to use an exported class inside an Angular2 component
So I've got this class in a separate file: 因此,我将此类放在单独的文件中:
export class MyModel {
votes: number;
title: string;
link: string;
constructor() {
this.title = 'Github';
///
}
myfunc()
///
}
And I want to use its props in this component, but can't figure out how to do it: 而且我想在此组件中使用其道具,但无法弄清楚该如何做:
import { Component } from '@angular/core';
import { MyModel } from './models.model';
@Component({
selector: ///
templateUrl: ///
styleUrls: ///
})
export class SomeComponent {
//declare somehow..?
}
You can declare it as below : 您可以如下声明:
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();
}
}
You can check app.component.ts
code on page angular tutorial 您可以在页面角度教程中查看app.component.ts
代码
You can simply declare an object using the new
keyword and then use that object to access title
and other attribute. 您可以简单地使用new
关键字声明一个对象,然后使用该对象访问title
和其他属性。
Example: 例:
var obj = new MyModel();
console.log(obj.title);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.