Preface
Angular2 uses TypeScript as the default encoding language, so everything you see is a file ending with .ts.
What is TypeScript
First of all, it is a compiled language; since it is compiled, then you can use tool attributes such as reconstruction, navigation, and intelligent reminder, so you will find that using VS CODE to write Angular is a perfect match.
At the same time, TypeScript also brings some ES6/7 features, such as let, const, async, etc. You don’t need to care about the ES number.
The most domineering, TypeScript is also a strong type, generic, polymorphic and other object-oriented programming things.
Then, why don’t we choose it?
Of course, after saying so much, it seems to have nothing to do with the topic, but if you understand this, you can see the essence of the problem.
First of all, the first key point is the compilation type. Since it is a compilation type, any variables, classes, and functions that appear in your code must exist, otherwise TA will report an error during compilation.
But, the problem is that there are so many ready-made third-party libraries in the JavaScript world today. Can't they be used? No!
TypeScript takes care of these from the beginning, so there will be a declaration file called . Of course MS won't let you write this file, so there is one called/The website, TA has collected many third-party statement files from ready-made libraries for us to download.
Method 1
Of course, it is the most formal army to use the command to install the jQuery declaration file.
npm install -D @types/jquery
And use
import * as $ from 'jquery'; $('body').addClass('');
Perfect smart tips if you are under VS CODE.
Method 2
What should we do for some class libraries that do not provide . declaration files? Of course, I can only write it myself.
What? Write it yourself? It's very difficult! Very complicated!
Not that true, the declaration file is actually a description of some class library interfaces. The following is part of the code I intercepted a piece of jQuery declaration file
interface JQueryStatic { /** * Remove the beginning and end spaces of string * * @param str string * @see {@link //} */ trim(str: string): string; } declare var $: JQueryStatic;
I also specially translated it into Chinese, and the content here is very small. The most important thing is to declare it defines a variable $ as type JQueryStatic (or an interface).
In this way, the TS compiler will look for the type when encountering $, and you cannot have $.time1() or something like this in your code, because there is only one interface$.trim()
。
Wait, do I have to write dozens of jQuery interfaces like this?
NO! ! ! Of course not, unless you want to write a statement file that is beautiful, beautiful, Chinese, and perfect smart tips.
Otherwise, you should take any type, it is a universal product. You don't need to write a very complicated declaration file, just:
declare var $: any;
Simple, crude and effective!
in conclusion
Alas~ Actually, it is because you can see a sentence in the group every day [How to use jQuery]; although the simplest result only requires one sentencedeclare var $: any
;, but I still talk a lot about it, but I don’t understand the causes and consequences. I’m annoyed.
In addition, this solution is just a way to attract jade, and is a common method in many library libraries. But I suggest to find some Angular2 class libraries to use, because how to manage JavaScript operations more effectively is a science.
Summarize
The above is the entire content of this article. I hope that the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.