简体   繁体   中英

Angular 5 Load CDN script tag from Angular-cli.json

I am using Angular 5 for the project.

My index.html looks like this :-

<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 ?

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

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. That can then be initiated with ng build --env=dev:

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

Then create the file 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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