简体   繁体   English

Angular 5从Angular-cli.json加载CDN脚本标签

[英]Angular 5 Load CDN script tag from Angular-cli.json

I am using Angular 5 for the project. 我正在为项目使用Angular 5。

My index.html looks like this :- 我的index.html看起来像这样:-

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>PDL Web</title>
    <link href="/assets/Images/favicon.ico" rel="shortcut icon" type="image/x-icon">
    <base id="baseHref" href="/">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <app-root></app-root>
</body>
</html>
<script src="https://cdn.ckeditor.com/4.11.2/standard-all/ckeditor.js"></script>

My question how to add the script tag of CDN resource from the angular-cli.json file ? 我的问题是如何从angular-cli.json文件中添加CDN资源的脚本标签?

Please suggest. 请提出建议。

You can probably leverage environment variables for this. 您可能可以为此使用环境变量。

Angular CLI projects already use a production environment variable to enable production mode when in the production environment Angular CLI项目已经在生产环境中使用生产环境变量来启用生产模式

It's easy to add new environments in Angular CLI projects by adding new entries to the environments field in the .angular-cli.json file. 通过在.angular-cli.json文件的环境字段中添加新条目,可以轻松地在Angular CLI项目中添加新环境。 That can then be initiated with ng build --env=dev: 然后可以使用ng build --env=dev:来启动ng build --env=dev:

"environments": {
  "dev": "environments/environment.ts",
  "prod": "environments/environment.prod.ts"
}

Then create the file environment.prod.ts : 然后创建文件environment.prod.ts

export const environment = {
  production: true,
  cdn: '/url/cdn.com'
};

Then add the script tag in your app-root html (don't forget to import the einvironment into you component.ts): 然后在您的应用程序根html中添加脚本标签(不要忘记将环境导入到您的component.ts中):

<script src="{{environment.cdn}}"></script>

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

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