My app by Angular simply starts with index.html :
(我的Angular应用只是以index.html开头:)
<body>
<app-root></app-root>
</body>
I would like it to be switched between Dark/Light by toggling between <body>
and <body class="dark-mode">
with below styles:
(我希望通过以下样式在<body>
和<body class="dark-mode">
之间切换来在暗/亮之间切换:)
body abcxyz {
color: #fff
}
body.dark-mode abcxyz {
color: #a2b3c4
}
Next is app.component.html with Header - Main - Footer as usual:
(接下来是app.component.html,其中“页眉-主-页脚”与往常一样:)
<app-header></app-header>
<router-outlet></router-outlet>
<app-footer></app-footer>
The header.component.html has a toggle button:
(header.component.html有一个切换按钮:)
<button (click)="onClickDarkMode()">Dark Mode</button>
We need a variable and a function like this to work, I put them in header.component.ts :
(我们需要一个变量和一个像这样的函数才能工作,我将它们放在header.component.ts中 :)
isDarkMode = false;
onClickDarkMode() {
this.isDarkMode = !this.isDarkMode;
}
This idea seems simple to implement, but the click event seems like it fires to nothing with any of these lines added to the <body>
:
(这个想法似乎很容易实现,但是click事件似乎在将任何以下行添加到<body>
时都不会触发:)
<body [class.dark-mode]="isDarkMode">
or
(要么)
<body [ngClass]="{'dark-mode': isDarkMode}">
or
(要么)
<body [ngClass]="isDarkMode ? 'dark-mode' : ''">
Further more, with the idea of "Dark/Light", is this the best way to implement by toggling class inside the <body>
?
(再者,以“暗/亮”的想法,这是在<body>
内部切换类的最佳实现方式吗?)
ask by nambk translate from so