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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Lazy Load, 延迟加载图片的 jQuery 插件.

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  4. jQuery延迟加载插件(Lazy Load)详解

    最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...

  5. Lazy Load, 延迟加载图片的 jQuery 插件 - NeoEase

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  6. Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute'

    [TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from as ...

  7. 延迟加载图片的 jQuery 插件:Lazy Load

    网站的速度非常重要,现在有很多网站优化的工具,如 Google 的Page Speed,Yahoo 的 YSlow,对于网页图片,Yahoo 还提供 Smush.it这个工具对图片进行批量压缩,但是对 ...

  8. Lazy Load 图片延迟加载(转)

    jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...

  9. jQuery Lazy Load 图片延迟加载

    基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...

随机推荐

  1. Quartz.Net—IJob特性

    IJob默认情况下是无状态的,和其他系统没有关系  特别是job里面的jobdata每次都是新的.可以无限扩展. PersistJobDataAfterExecution JobData持久化 Job ...

  2. 《Mysql - 如何恢复和避免误删除?》

    一:误删数据 (如何恢复和避免误删除) - 使用 delete 语句误删数据行: - 使用 drop table 或者 truncate table 语句误删数据表: - 使用 drop databa ...

  3. golang之方法

    golang中的方法是作用在指定的数据类型上的(即:和指定 数据类型绑定),因此自定义类型,都可以有方法,而不仅仅是在struct. 方法的使用: type Person struct { Num i ...

  4. teamviewer远程控制程序免费版百度云下载

    TeamViewer是一个远程共享桌面软件,使远程传输变得简单快速,远程访问安全可靠,能在任何防火墙后台进行远程控制.只需用户在两台计算机上同时运行这个软件就可以开始工作.使用时关闭杀毒软件,防止误报 ...

  5. Java版Kafka使用及配置解释

    Java版Kafka使用及配置解释 一.Java示例 kafka是吞吐量巨大的一个消息系统,它是用scala写的,和普通的消息的生产消费还有所不同,写了个demo程序供大家参考.kafka的安装请参考 ...

  6. Asp.Net 加载不同项目程序集

    我们做项目时有时候不想添加别的项目的引用,但是那个项目又必须在 Global 中进行注册 最常见的就是插件机制,参考: https://shazwazza.com/post/Developing-a- ...

  7. element-ui 文件上传

    <el-form-item> <el-upload ref="upload" class="upload-demo" :action=&quo ...

  8. vue进入页面时不在顶部,检测滚动返回顶部按钮

    这里是本小白使用时遇到的问题及个人使用的方法可能并不完美. 1.监测浏览器滚动条滚动事件及滚动距离 dmounted() { window.addEventListener("scroll& ...

  9. Java 之 IO 异常的处理【了解】

    一.JDK7 前的处理 前面的 Demo 中,一直把异常抛出,而在实际中并不能这样处理,建议使用 try...catch...finally 代码块,处理异常部分. 格式: try{ 可能会产出异常的 ...

  10. Python 自己实现可迭代对象

    import time from collections import Iterable from collections import Iterator class Classmate(object ...