本文介绍了在Angular2上向body添加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个组成部分。这些是HomeComponent,SignInComponent和AppComponent。应用程序打开时,我的主页(HomeComponent)显示。我点击登录页面点击登录按钮。我打开它时想要登录页面课程。
I have three components. These are HomeComponent, SignInComponent and AppComponent. My Home Page (HomeComponent) is showing when the application opened. I clicked the "Sign In" button by signin page opens.I want "signin-page" class to body while opening it.
我该怎么办?
// AppComponent
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app',
template: '<router-outlet></router-outlet>'
})
export class AppComponent {}
// SignInComponent
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'signin',
templateUrl: './signin.component.html',
styleUrls: ['./signin.component.css']
})
export class SignInComponent {}
// HomeComponent
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'home',
templateUrl: './home.component.html'
})
export class HomeComponent { }
// Part of index.html
<body>
<app>
<div class="spinner">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</app>
</body>
推荐答案
您可以将根选择器更改为 body
然后使用 HostBinding
decorator
You can change your root selector to body
and then use HostBinding
decorator
@Component({
selector: 'body',
template: `<child></child>`
})
export class AppComponent {
@HostBinding('class') public cssClass = 'class1';
}
@Component({
selector: 'child',
template: `<button (click)="setClass()">Set class</button>`
})
export class ChildComponent {
constructor(private rootComp: AppComponent) { }
setClass() {
this.rootComp.cssClass = 'class2';
}
}
这篇关于在Angular2上向body添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!