如何通过 Angular 中的 url 动态更改 class
how to change class dynamically with url change in Angular
我在 app.html 上有一个仪表板组件,我想显示其中的所有内容,但在登录或注册时除外 URL。所以我正在尝试动态 add/remove 一个定义为 main__hidden
的 class,它根据 URL 对 display: none
进行阻塞。但由于某种原因,它不起作用。我也试过在全局 style.css 中定义样式,但没有用。
app.html
<app-main *ngIf="showNavbar">
<router-outlet></router-outlet>
</app-main>
app.ts
export class AppComponent {
title = 'frontend';
public showNavbar: boolean = false
constructor(private router: Router) {
// Removing Sidebar, Navbar, Footer for Documentation, Error and Auth pages
router.events.forEach((event) => {
if (event instanceof NavigationStart) {
if ((event['url'] == '/sign-in') || (event['url'] == '/sign-up')) {
this.showNavbar = false;
let body = document.querySelector('body');
body.classList.add('main__hidden');
}
} else {
this.showNavbar = true;
let body = document.querySelector('body');
body.classList.remove('main__hidden');
}
});
}
}
main.css
.main__header {
z-index: 1;
position: fixed;
background: #22242a;
padding: 20px;
width: calc(100% - 0%);
top: 0;
height: 30px;
display: block;
}
.main__sidebar {
z-index: 1;
top: 0;
background: #2f323a;
margin-top: 70px;
padding-top: 30px;
position: fixed;
left: 0;
width: 250px;
height: 100%;
transition: 0.2s;
transition-property: left;
overflow-y: auto;
display: block;
}
:host-context(.main__hidden) .main__sidebar {
display: none;
}
:host-context(.main__hidden) .main__header {
display: none;
}
在 main.ts 中创建方法 showMain()
并在构造函数中调用它
public showMain = () => {
this.router.events.forEach((event) => {
if (event instanceof NavigationStart) {
if ((event['url'] == '/sign-in') || (event['url'] == '/sign-up')) {
this.hideMain = false;
document.querySelector('.main__header').classList.add('main__hidden');
document.querySelector('.main__sidebar').classList.add('main__hidden');
} else {
this.hideMain = true;
document.querySelector('.main__header').classList.remove('main__hidden');
document.querySelector('.main__sidebar').classList.remove('main__hidden');
}
}
});
}```
我在 app.html 上有一个仪表板组件,我想显示其中的所有内容,但在登录或注册时除外 URL。所以我正在尝试动态 add/remove 一个定义为 main__hidden
的 class,它根据 URL 对 display: none
进行阻塞。但由于某种原因,它不起作用。我也试过在全局 style.css 中定义样式,但没有用。
app.html
<app-main *ngIf="showNavbar">
<router-outlet></router-outlet>
</app-main>
app.ts
export class AppComponent {
title = 'frontend';
public showNavbar: boolean = false
constructor(private router: Router) {
// Removing Sidebar, Navbar, Footer for Documentation, Error and Auth pages
router.events.forEach((event) => {
if (event instanceof NavigationStart) {
if ((event['url'] == '/sign-in') || (event['url'] == '/sign-up')) {
this.showNavbar = false;
let body = document.querySelector('body');
body.classList.add('main__hidden');
}
} else {
this.showNavbar = true;
let body = document.querySelector('body');
body.classList.remove('main__hidden');
}
});
}
}
main.css
.main__header {
z-index: 1;
position: fixed;
background: #22242a;
padding: 20px;
width: calc(100% - 0%);
top: 0;
height: 30px;
display: block;
}
.main__sidebar {
z-index: 1;
top: 0;
background: #2f323a;
margin-top: 70px;
padding-top: 30px;
position: fixed;
left: 0;
width: 250px;
height: 100%;
transition: 0.2s;
transition-property: left;
overflow-y: auto;
display: block;
}
:host-context(.main__hidden) .main__sidebar {
display: none;
}
:host-context(.main__hidden) .main__header {
display: none;
}
在 main.ts 中创建方法 showMain()
并在构造函数中调用它
public showMain = () => {
this.router.events.forEach((event) => {
if (event instanceof NavigationStart) {
if ((event['url'] == '/sign-in') || (event['url'] == '/sign-up')) {
this.hideMain = false;
document.querySelector('.main__header').classList.add('main__hidden');
document.querySelector('.main__sidebar').classList.add('main__hidden');
} else {
this.hideMain = true;
document.querySelector('.main__header').classList.remove('main__hidden');
document.querySelector('.main__sidebar').classList.remove('main__hidden');
}
}
});
}```