如何在 angular 2 组件打字稿文件中添加类属性?

Kap*_*dav 2 angular

如何在 angular 2 组件打字稿文件中添加类属性?

下面是我的页脚组件代码,我想向它添加页脚类。

页脚.component.ts

import { Component } from '@angular/core';
import { Footer }    from './footer';
@Component({
  moduleId: module.id,
  selector: 'footer',
  templateUrl: 'footer.component.html'
})
export class FooterComponent {
constructor() {
    this.title = 'Footer Content';
    this.css_class = 'navbar-fixed-bottom';
  }
}
Run Code Online (Sandbox Code Playgroud)

页脚.ts

export class Footer {
  constructor(
    public title: string
  ) {  }
}
Run Code Online (Sandbox Code Playgroud)

请建议。

小智 6

您可以使用hostComponent 装饰器的metadata 属性。 https://angular.io/docs/ts/latest/api/core/index/Component-decorator.html

你的组件装饰器应该是这样的:

@Component({
  moduleId: module.id,
  selector: 'footer',
  templateUrl: 'footer.component.html',
  host: {'class': 'my-footer'}
})
Run Code Online (Sandbox Code Playgroud)