[Angular] Lazy Load CSS at runtime with the Angular CLI

Ever had the need for multiple "app themes", or even to completely dynamically load CSS based on which customer logs into your application? You could of course bundle all of the various themes into a single CSS entry file, but your application size would suffer a lot. Therefore, in this lesson we're going to have a look how to define multiple entry-level CSS files and how to "lazy load" them at runtime.

 

Source: https://egghead.io/lessons/angular-lazy-load-css-at-runtime-with-the-angular-cli

 

For example we want to lazy load two theme file: 'client-a-style.scss', 'client-b-style.scss':

In angular.json:

"architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/dyncss",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": ["src/favicon.ico", "src/assets"],
            "extractCss": true,
            "styles": [
              "src/styles.scss",
              {
                "input": "src/client-a-styles.scss",
                "bundleName": "client-a",
                "inject": false
              },
              {
                "input": "src/client-b-styles.scss",
                "bundleName": "client-b",
                "inject": false
              }
            ],
            "scripts": []
          },

  

After you do `ng build --prod`, it will generate two css files: 'client-a.css' and 'client-b.css'.

Then we can do lazy load when button click:

import { Component, Inject, Renderer2 } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'dyncss';
  // reference document
  constructor(@Inject(DOCUMENT) private document: Document) {}

  loadStyle(styleName: string) {
    // get head
    const head = this.document.getElementsByTagName('head')[0];

    // create link tag to load css
    let themeLink = this.document.getElementById(
      'client-theme'
    ) as HTMLLinkElement;
    if (themeLink) {
      // if the link is already exist, we just replace the link source
      themeLink.href = styleName;
    } else {
      // if link doesn't exist, we create link tag
      const style = this.document.createElement('link');
      style.id = 'client-theme';
      style.rel = 'stylesheet';
      style.href = `${styleName}`;

      head.appendChild(style);
    }
  }
}
<button type="button" (click)="loadStyle('client-a.css')">
  Load client style a
</button>
<button type="button" (click)="loadStyle('client-b.css')">
  Load client style b
</button>

  

转载于:https://www.cnblogs.com/Answer1215/p/11395027.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值