search
HomeWeChat AppletMini Program DevelopmentHow to use npm package functions in WeChat applet development?

How to use npm package functions in WeChat applet development?

Apr 04, 2020 am 09:37 AM
npmWeChat applet development

How to use npm package functions in WeChat applet development?

This article introduces how to use the npm package function in the development of WeChat mini programs, which greatly improves the development efficiency of WeChat mini programs. It is also an update to the video version of the WeChat mini program series tutorials.

WeChat Mini Program did not support npm when it was first released. This is also the point where many front-end developers are currently criticizing WeChat Mini Program after becoming familiar with the npm ecological environment.

The WeChat mini program added support for npm package loading after version 2.2.1, so that the mini program supports using npm to install third-party packages.

The function updates of WeChat Mini Programs have been updated recently and are very powerful. For example, the new function "Cloud Development" of the WeChat Mini Program Development Platform introduced to you before can be quickly used to experience.

Loading npm packages in mini programs

WeChat mini programs use the official documentation for loading npm packages here. In this practical part, we load a third party of npm. The library miniprogram-datepicker is used to implement the date selection function between the Gregorian calendar and the lunar calendar, while the official component of the WeChat applet can only simply select a Gregorian calendar time.

How to use npm package functions in WeChat applet development?

Locate the project folder of the WeChat applet in the terminal and install it through the npm installation command.

How to use npm package functions in WeChat applet development?

Be sure to use the –production option here, which can reduce the need to install some non-business npm packages, thereby reducing the size of the entire small program package.

Build npm package

Click the "Build npm" command under the "Tools" menu of the WeChat applet development tool to build the npm package. This build can npm packages are built into packages that can be loaded and used in small programs.

In order to help everyone better understand the various requirements mentioned in publishing npm packages, here is a brief introduction to the principle:

First of all, the node_modules directory will not participate in compilation, uploading and packaging, so If a mini program wants to use the npm package, it must go through the process of "building npm". A miniprogram_npm directory will be generated under the same level directory of the outermost node_modules, which will store the built and packaged npm package, which is what the mini program actually uses. npm package.

Building and packaging are divided into two types: the mini program npm package will directly copy all the files in the build file generation directory to miniprogram_npm; other npm packages will go through the dependency analysis and packaging process starting from the entry js file ( similar to webpack).

The process of finding npm packages is similar to the implementation of npm. Start from the directory where the files that depend on npm packages are located and search layer by layer until you find an available npm package or the root directory of the applet.

How to use npm package functions in WeChat applet development?

#After the build is completed, you need to confirm that the project has checked "Use npm module".

How to use npm package functions in WeChat applet development?

Using third-party modules in the project

In our previous actual project, we tested this npm module on the homepage load.

After laying out datepicker on the page, save and complete the compilation of the project, click the button to see the loading status of the component.

How to use npm package functions in WeChat applet development?

Recommended: "小program Development Tutorial"

The above is the detailed content of How to use npm package functions in WeChat applet development?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:即速资讯. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft