How to develop WeChat applet with TypeScript

高洛峰
Release: 2017-02-16 10:03:18
Original
2080 people have browsed it

WeChat mini program is here! Although this thing that claims to kill traditional apps is currently in the internal beta stage, the official documentation of the application account has released an emulator that can be used without an internal beta account.

TypeScript:

TypeScript is another masterpiece of Anders Hejlsberg, the father of C#. I believe that friends who like C# syntax will also love TypeScript.

Let’s talk briefly about TypeScript

  • TS is an application-level JavaScript development language.

  • TS is a superset of JavaScript and can be compiled into pure JavaScript.

  • TS is cross-browser, cross-operating system, cross-host, open source.

  • TS begins with JS and ends with JS. Following the syntax and semantics of JavaScript makes it convenient for countless JavaScript developers.

  • TS can reuse existing JavaScript code and call popular JavaScript libraries.

  • TS can be compiled into concise, simple JavaScript code that can run on any browser, Node.js, or any ES3-compatible environment.

  • TypeScript is more efficient in development than JavaScript, including: static type checking, symbol-based navigation, automatic statement completion, code refactoring, etc.

  • TS provides classes, modules and interfaces to make it easier to build components.

By the way, although TypeScript only cares about the content before generating JavaScript (which means it does not care about the running efficiency of the generated JS code), according to my observation and comparison, TypeScript The quality of the generated JavaScript code is at least one order of magnitude higher than the JavaScript code written by most front-end developers! !

Another advantage of TypeScript:

TypeScript has smart prompts in all major IDEs and editors!

Say important things three times! There are smart tips for writing TypeScript! There are smart tips for writing TypeScript! There are smart tips for writing TypeScript!

Using TypeScript to develop WeChat mini programs

I have been talking about TypeScript for a long time, so how do I use TypeScript to develop WeChat mini programs?

Very simple, not much different from WeChat’s official JavaScript development method, it is still 4 core files

  • App: Code is the abstract object of the entire application, you can Set global methods and variables

  • Page: Page abstract object, carrying page business logic

  • ##WXML: Page structure, equivalent to html

  • JSON: Configuration file

  • WXSS: Page style, equivalent to css

Due to the current Tencent does not have a TypeScript version of the API for mini programs, so the OneCode team developed a TypeScript version of the API type definition file for all mini program JavaScript APIs currently released by Tencent

wxAPI.d.ts

You only need to reference this file in your program. If you use Visual Studio to develop, you will have code prompts.

如何用TypeScript开发微信小程序

The following is a code example of a Demo App developed with TypeScript:


/// App({ onLaunch: function() { //调用API从本地缓存中获取数据 let logs: any = wx.getStorageSync('logs'); if (!Array.isArray(logs)) { logs = []; } (logs).unshift(Date.now()); wx.setStorageSync('logs', logs); }, getUserInfo: function(cb: (param: any) => void) { let that = this if (this.globalData.userInfo) { cb(this.globalData.userInfo) } else { //调用登录接口 wx.login({ success: () => { wx.getUserInfo({ success: (res) => { that.globalData.userInfo = res.userInfo; cb(that.globalData.userInfo); } }); } }); } }, globalData: { userInfo: null } });
Copy after login
For more related articles on how to use TypeScript to develop WeChat applets, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!