Angular 7开发配置中的样式表仅限于开发环境。
创始人
2024-10-17 02:30:30
0

在Angular 7中,可以使用一个环境变量来设置样式表仅在开发环境中生效。以下是一个解决方法的代码示例:

  1. 首先,在src/environments目录下创建两个环境变量文件:environment.tsenvironment.prod.ts

  2. environment.ts文件中添加以下内容:

export const environment = {
  production: false,
  styles: [
    'src/styles.css',
    // 添加其他开发环境的样式表
  ]
};
  1. environment.prod.ts文件中添加以下内容:
export const environment = {
  production: true,
  styles: [
    // 只在开发环境使用的样式表不需要添加
  ]
};
  1. angular.json文件中,找到architect.build.optionsarchitect.serve.options字段,将它们的styles属性的值更改为以下内容:
"styles": [
  "src/styles.css",
  ...environment.styles
],
  1. src/styles.css文件中,添加以下代码以限制样式表仅在开发环境中生效:
:host-context(.production) {
  display: none !important;
}
  1. src/app/app.component.ts文件中,添加以下代码以根据环境变量为根元素添加production类:
import { Component, OnInit } from '@angular/core';
import { environment } from '../environments/environment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  ngOnInit() {
    if (environment.production) {
      document.documentElement.classList.add('production');
    }
  }
}

通过这种方式,只有在开发环境中,src/styles.css样式表才会被加载并应用到组件中。在生产环境中,src/styles.css样式表将被忽略,并且production类将被添加到根元素,从而使@host-context(.production)选择器生效。

相关内容

热门资讯

一起来探讨!wepoker好友... 此外,数据分析德州(wepoker好友房开挂)辅助神器app还具备辅助透视行为开挂功能,通过对客户w...
科技介绍!pokemmo脚本辅... 科技介绍!pokemmo脚本辅助器,wepoker数据分析工具,微扑克教程(有挂教程)准备好在wep...
重磅来袭!aapoker透视脚... 重磅来袭!aapoker透视脚本下载,wepoker私人定制透视,介绍教程(有挂方法)是一款可以让一...
发现玩家!aapoker怎么开... 《发现玩家!aapoker怎么开辅助器,sohoo poker辅助器,玩家教程(有挂技巧)》 aap...
技巧知识分享!wepoker辅... 技巧知识分享!wepoker辅助插件功能,hhpoker辅助挂,新版2025教程(有挂透明)关于we...
总算清楚!wepoker可以透... 总算清楚!wepoker可以透视码,we-poker辅助软件教程,必备教程(有挂技巧)1、点击下载安...
重大通报!wepoker底牌透... 1、重大通报!wepoker底牌透视,拱趴大菠萝有挂吗,必赢方法(有挂软件);详细教程。2、拱趴大菠...
推荐十款!wepoker国外版... 此外,数据分析德州(wepoker怎么挂底牌)辅助神器app还具备辅助透视行为开挂功能,通过对客户w...
揭秘!wepoker透视脚本是... 揭秘!wepoker透视脚本是什么,werplan怎么作弊,透明挂教程(有挂透明)1、点击下载安装,...
今日焦点!智星菠萝可以辅助吗,... 今日焦点!智星菠萝可以辅助吗,wepoker私局辅助,普及教程(有挂透视)1、点击下载安装,微扑克w...