Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular include CDN in component usage

I have CDN URL and I want to use it inside of the Component TypeScript file.

What would be the "right" way of dealing with CDN's in Angular 2 and greater versions?

like image 244
Volodymyr Prysiazhniuk Avatar asked Jul 23 '18 17:07

Volodymyr Prysiazhniuk


People also ask

Can I use CDN in angular?

AngularJs CDN is ideal for delivering the files efficiently, and it allows the user to retrieve the data from the most optimal CDN server rather than from the original server. The AngularJs CDN integration has so many advantages, such as HTTP/2-supported servers, CORS, an extensive network of data centers, etc.

In which tag we include AngularJS CDN?

AngularJS module This module is able to prepend the CDN server paths (e.g. cdn.example.com ) to the <img> tag src attribute. This AngularJS CDN integration method allows for your images to be easily delivered via a CDN instead of locally from the origin server.

What does Includes do in angular?

AngularJS ng-include Directive The ng-include directive includes HTML from an external file. The included content will be included as childnodes of the specified element. The value of the ng-include attribute can also be an expression, returning a filename.


1 Answers

If you want users to retrieve the js/css files from a CDN, you need to include these files in your index.html. Example with momentjs (Note: this is just an example, momentjs can be installed via npm in your project)

index.html

<!-- get script from CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js" />

Then, to use the the script in your component, you need to manually declare the exported variables / functions to avoid TS compilation errors

component.ts

declare let moment: any; //declare moment

//use moment
moment().format(....);

Note: some libraries have types that you can use instead, instead of using any. You can get these types from the @types repository

like image 158
David Avatar answered Nov 04 '22 16:11

David