[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 ...
随机推荐
- 使用TypeScript创建Vue项目
Vue的灵活性总是让代码看起来非常洗练,对TypeScript来说也是一种挑战, 好在Vue对TypeScript进行了一次全方位的适配. 相对于React严谨的代码,Redux啰嗦的样板代码,Vue ...
- c++ 编译 curl 报错 数组‘__curl_rule_01__’的大小为负 解决方法
背景:在原有的项目GCC编译环境下(arm-linux 32位),增加x86-linux 64位的编译环境,编译curl库的时候发生错误. 其他:编译服务器为64位Centos 编译错误提示 /inc ...
- 以环形角度理解php数组索引
array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] ) : ...
- youku项目总结(粗略总结)
一.ORM 之前我们都是以文件保存的形式存储数据,这次我们用的是数据库结合python使用,用到 ORM:关系型映射 类>>数据库的一张表 对象>>表一条记录 对象.属性> ...
- sequence(线段树+单调栈) (2019牛客暑期多校训练营(第四场))
示例: 输入: 31 -1 11 2 3 输出: 3 题意:求最大的(a区间最小值*b区间和) 线段树做法:用单调栈求出每个数两边比b数组大的左右边界,然后用线段树求出每段区间的和sum.最小前缀ls ...
- 2019牛客暑期多校训练营(第四场)A meeting(dfs或dp,dp待更新)
示例1: 输入: 4 21 23 13 42 4 输出:2 说明: They can meet at place 1 or 3. 题意:从K个点到达不联通图某个点需要的最短时间,这个最短时间是这K个人 ...
- spring的事务解决方案之@Transactional注解
首先此注解位于 org.springframework.transaction.annotation 这个包路径下面, 事务有两种类别,一种是编程式事务,另一种是声明式事务,显然此注解是声明式事务,这 ...
- vue-cli 3.0 端口变5位数
问题原因 : portfinder1.0.22版本导致vue-cli-service servey启动后,服务端口变成了随机端口的问题 解决办法: 删了目录 下的node-modules 然后 执 ...
- ubuntu下使用eclipse调试jni无法获取环境变量,本地库(java.library.path,LD_LIBRARY_PATH)等问题的解决。
首先要把本地库全部配置到LD_LIBRARY_PATH中. 然后一定要采用命令行方式启动eclipse(也可以写一个启动shell,通过桌面启动器打开这个shell),这样环境变量才会有效. 打开终端 ...
- gulp删除目标文件中所有的console.log()语句——gulp-strip-debug
1.安装npm包 npm install --save-dev gulp-strip-debug 2.使用 const gulp = require('gulp'); const stripDebug ...