我有三个组成部分 . 这些是HomeComponent,SignInComponent和AppComponent . 应用程序打开时,我的主页(HomeComponent)显示 . 我点击登录页面打开“登录”按钮 . 我打开它时想要“登录页面”类到正文 .
我该怎么做?
// 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>
4 回答
您可以将根选择器更改为
body
,然后使用HostBinding
装饰器Angular2没有提供任何内置方法来修改根组件之外的DOM元素(
<title>
除外) .要么
也可以看看
How do I add a class to a given element?
Angular 2.x bind class on body tag
1. If part of your application is in angular, then you can do this-
let body = document.getElementsByTagName('body')[0];
body.classList.remove( “类名”); //删除课程
body.classList.add( “类名”); //添加课程
2. I'll prefer answer given by @Yurzui if whole front end is in angular.
很简单,你可以这样做来添加一个类到正文