[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>
[Angular] Lazy Load CSS at runtime with the Angular CLI的更多相关文章
- [Angular2 Router]  Lazy Load Angular 2 Modules with the Router
		
Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start ...
 - [Vuex] Lazy Load a Vuex Module at Runtime using TypeScript
		
Sometimes we need to create modules at runtime, for example depending on a condition. We could even ...
 - Lazy Load, 延迟加载图片的 jQuery 插件.
		
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
 - jQuery延迟加载插件(Lazy Load)详解
		
最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...
 - Lazy Load, 延迟加载图片的 jQuery 插件 - NeoEase
		
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
 - Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute'
		
[TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from as ...
 - 延迟加载图片的 jQuery 插件:Lazy Load
		
网站的速度非常重要,现在有很多网站优化的工具,如 Google 的Page Speed,Yahoo 的 YSlow,对于网页图片,Yahoo 还提供 Smush.it这个工具对图片进行批量压缩,但是对 ...
 - Lazy Load 图片延迟加载(转)
		
jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...
 - jQuery Lazy Load 图片延迟加载
		
基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...
 
随机推荐
- [转帖]nginx sendfile tcp_nopush tcp_nodelay参数解释
			
nginx sendfile tcp_nopush tcp_nodelay参数解释 2013-06-25 13:59:40 zmj_88888888 阅读数 20425 文章标签: nginxtcp_ ...
 - Python 装饰器执行顺序
			
Python 装饰器执行顺序 之前同事问到两个装饰器在代码中使用顺序不同会不会有什么问题,装饰器是对被装饰的函数做了一层包装,然后执行的时候执行了被包装后的函数,例如: def decorator_a ...
 - Git服务器搭建--ssh/http
			
测试环境 Windows 7 Ultimate, 64-bit 6.1.7601, Service Pack 1(实体机,虚拟机VMware的宿主机) VMware® Workstation 7.1. ...
 - Python-20-异常处理
			
一.什么是异常 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止) 常用异常: AttributeError 试图访问一个对 ...
 - Django REST  framework 基本组件
			
一.序列化组件 简单使用 开发我们的Web API的第一件事是为我们的Web API提供一种将代码片段实例序列化和反序列化为诸如json之类的表示形式的方式.我们可以通过声明与Django forms ...
 - SAS学习笔记56 ODS ESCAPECHAR
			
这种内嵌格式独立于style型和table型,它既可以结合二者使用,也可以独立使用.它主要通过下列语句的格式形式来进行调用: ODS ESCAPECHAR ‘^’; 上述符号’^’表示触发条件,如果碰 ...
 - java 线程并发(生产者、消费者模式)
			
线程并发协作(生产者/消费者模式) 多线程环境下,我们经常需要多个线程的并发和协作.这个时候,就需要了解一个重要的多线程并发协作模型“生产者/消费者模式”. Ø 什么是生产者? 生产者指的是负责生产数 ...
 - WinForm 无焦点获取键盘输入
			
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
 - JS如何做2048(详细)
			
在做2048之前,我们首先要了解它的游戏规则,以及运行逻辑 首先,来看上半部分 除了标题外还有记录每次获得的分数,以及总分数,还有一个重新开始按钮,这个最大分数会保存下来. 来看页面内容 页面内容由1 ...
 - ubuntu18.04 安装android studio
			
首先从官网下载android studio:Android Studio (安装前应先安装JDK环境) 得到android-studio-ide-191.5977832-linux.tar.gz 在安 ...