博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular] Lazy Load CSS at runtime with the Angular CLI
阅读量:4983 次
发布时间:2019-06-12

本文共 2579 字,大约阅读时间需要 8 分钟。

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: 

 

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);    }  }}

  

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

你可能感兴趣的文章
向量非零元素个数_向量范数详解+代码实现
查看>>
LeetCode 题解之Add Digits
查看>>
hdu1502 , Regular Words, dp,高精度加法
查看>>
iOS 电话在后台运行时,我的启动图片被压缩
查看>>
js --基本语法3 函数,数组,堆棧
查看>>
Oracle 数据库导入、导出
查看>>
批量修改 表结构
查看>>
MySQL的btree索引和hash索引的区别
查看>>
抽象类和接口有什么区别
查看>>
wc2018
查看>>
[转载] 杜拉拉升职记——01 忠诚源于满足
查看>>
那些mv*框架如何选择
查看>>
git工作流程
查看>>
Excel坐标自动在AutoCad绘图_3
查看>>
hacknet
查看>>
HTML语义化初探
查看>>
Peaceful Commission 2-sat
查看>>
bzoj3810: [Coci2015]Stanovi(记忆化搜索)
查看>>
azkaban调度
查看>>
11、增强型for循环对二维数组的输出(test8.java)
查看>>