简体   繁体   English

asp.net核心 <environment> 等效于angular2?

[英]asp.net core <environment> equivalent in angular2?

In asp.net core, I can conditionally include css/js in my layout html page using <environment> tags: 在asp.net核心中,我可以使用<environment>标签有条件地在布局html页面中包含css / js:

 <environment names="Development,Staging"> <script type="text/javascript" href="js/debug.js"></script> <link rel="stylesheet" type="text/css" href="css/style.css" /> </environment> <environment names="Production"> <link rel="stylesheet" type="text/css" href="css/style.min.css" /> </environment> 

How can I achieve this in angular 2? 如何在角度2中做到这一点? The css/js files that I'm talking about are site-wide, not component-specific 我正在谈论的css / js文件是整个站点范围内的,而不是特定于组件的

(PS. I'm new to angular 2 ) (PS。 我是角度2的新手

If you are using angular2 CLI then you could use enironment.ts . 如果你正在使用angular2 CLI那么你可以使用enironment.ts Properties specified in this file will be available throughout entire application. 该文件中指定的属性将在整个应用程序中可用。

You can create multiple environments like this- 您可以创建多个这样的环境-

在此处输入图片说明

In components import default environment file like this- 在组件中导入默认环境文件,如下所示:

import { environment } from '../../environments/environment';

Import DOCUMENT from platform-browser like this- 像这样从平台浏览器导入DOCUMENT-

import { DOCUMENT } from '@angular/platform-browser';

Inject into component (eg main AppComponent), 注入组件(例如主AppComponent),

constructor (@Inject(DOCUMENT) private document) { }

Use environment condition and apply dynamic style-sheet like this- 使用环境条件并像这样应用动态样式表-

if(environment.EnvName === 'prod') {
        this.document.getElementById('theme').setAttribute('href', 'prod.css');
}
else {
        this.document.getElementById('theme').setAttribute('href', 'dev.css');
}

Angular CLI takes care of which file to use for each environment during build process. Angular CLI负责在构建过程中为每个环境使用哪个文件。

This is how you can specify environment at a time build- 通过这种方式,您可以一次指定环境-

ng build --env=prod

Hope this helps in your use case. 希望这对您的用例有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM