search
HomeWeChat AppletMini Program DevelopmentHow to convert the default style unit px to rpx in the Vant component of the mini program

This article will introduce to you how to convert px to rpx when using the Vant component in WeChat Mini Program. I hope it will be helpful to everyone!

How to convert the default style unit px to rpx in the Vant component of the mini program

When the WeChat applet uses a third-party component library (for example: Vant), the default style of the component is px, which is not compatible with the rpx unit in our page . [Related learning recommendations: 小program development tutorial]

Technical solution


1.gulp

2.gulp-postcss

3.postcss-px2units

Implementation steps

1. Initialize and install

npm init //一路回车 
npm install --production 
npm i @vant/weapp -S --production

2. Check npm

How to convert the default style unit px to rpx in the Vant component of the mini program

in the WeChat Mini Program Development Tool. 3. Build npm

How to convert the default style unit px to rpx in the Vant component of the mini program# in the WeChat Mini Program Development Tool.

##4. After the build is successful, the miniprogram_npm folder will appear

How to convert the default style unit px to rpx in the Vant component of the mini program

#5. Install the npm package

npm install gulp gulp-postcss postcss-px2units --save-dev

6. Create a new gulpfile in the root directory. js file

var gulp = require('gulp');
var postcss = require('gulp-postcss');
var pxtounits =  require('postcss-px2units');

gulp.task('css', function () {
  return gulp.src(['miniprogram_npm/@vant/weapp/**/*.wxss'])
    .pipe(postcss([pxtounits({
      multiple: 2,
      targetUnits: 'rpx'
    })]))
    .pipe(gulp.dest('miniprogram_npm/@vant/weapp/'));
});

7. Add an execution command to the scripts under package.json

 "scripts": {
    "build": "gulp css",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

8. Execute in the command line

npm run build

9. View the converted wxss file

How to convert the default style unit px to rpx in the Vant component of the mini program

The px has been converted into rpx, and you're done! ! !

For more programming-related knowledge, please visit:

Programming Learning! !

The above is the detailed content of How to convert the default style unit px to rpx in the Vant component of the mini program. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.