Angular 2 : Component host property

标签 angular

@Component.host 属性代表什么?

根据 Angular2 documentation它代表:

host - map of class property to host element bindings for events, properties and attributes.

我不太明白这是干什么用的?

我提出这个问题是为了理解我最近几天遇到的一段代码。

代码是:

@Component({
  selector: 'layout',
  encapsulation: ViewEncapsulation.None,
  templateUrl: './layout.template.html',
  host: {
    '[class.nav-static]' : 'config.state["nav-static"]',
    '[class.chat-sidebar-opened]' : 'chatOpened',
    '[class.app]' : 'true',
    id: 'app'
  }
})
export class Layout {

最佳答案

我已将类添加到主机标签。

喜欢以下内容:

  • 组件

    @Component({
      selector: 'mytag',
      templateUrl: './layout.template.html',
      host: {
        'class' : 'myclass1 myclass2 myclass3'
      }
    })
    export class MyTagComponent {
    
  • 查看代码

    <mytag></mytag>

  • 结果

    <mytag class="myclass1 myclass2 myclass3"></mytag>

关于 Angular 2 : Component host property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41161088/

相关文章:

reactjs - 如何在 momentjs 中获取 endOf day toISOString() 但毫秒应该是 .000Z 而不是 .999Z

html - 如何使用 Tab 键使 Toggle 按钮可聚焦

angular - 属性在类型上不存在

angular - 如何在 Angular 4 中使用 CryptoJS

angular - 将 Angular 验证类应用于非 FormControl 父级

html - Cucumber 可以用于表单测试吗

angular - 在路由解析器内使用订阅

angular - 使用renderer.js和 Angular 组件使用的 Angular/Electron

angular - 如何测试依赖于另一个服务的服务/组件又依赖于 Http 服务?

angular - 从 IpcMain 通信时没有 Angular 变化检测