Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...