重点是,主题通常只是一些不同的颜色。您可以将所有主题添加到一个CSS。 my-app > .dark
获取不同的颜色,并且该类将在代码中动态添加。
这里有一个更详细的例子,我如何使用它:
APP-theme.scss:
@import '[email protected]/material/theming';
@include mat-core();
$words-app-primary: mat-palette($mat-teal, 300);
$words-app-secondary: mat-palette($mat-indigo, 500);
$words-app-warn: mat-palette($mat-red, 500);
$words-app-theme: mat-light-theme($words-app-primary, $words-app-secondary, $words-app-warn);
@include angular-material-theme($words-app-theme);
app-words-root > md-sidenav-container.dark {
$words-app-theme-dark: mat-dark-theme($words-app-primary, $words-app-secondary, $words-app-warn);
@include angular-material-theme($words-app-theme-dark);
}
app.component.html:
<md-sidenav-container [class.dark]="useDarkTheme"> ... </md-sidenav-container>
非角度版
app.component.js
import { DOCUMENT } from '@angular/platform-browser';
class AppComponent {
constructor(@Inject(DOCUMENT) document: any) {
let head = document.getElementsByTagName('head')[0];
let link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'assets/dark_room.css'; // you may want to get this from local storage or location
head.appendChild(link);
}
}
您是否试过这种方法? 让您的后端根据URL参数生成不同的index.html。 – Niklas
@Niklas我认为这可能是一种方法,但我不知道如何。因为设置URL参数通常在.ts文件中完成,但index.html没有。 –