Getting Started with WePY: Build WeChat Mini‑Programs Faster
This guide introduces the WePY framework, showing how to install the CLI, create a demo project, configure the build, understand .wpy file structure, and leverage components, data binding, and event communication to develop WeChat mini‑programs efficiently.
WePY is a framework that wraps native WeChat mini‑program development with a Vue‑like syntax and MVVM architecture, supporting ES6/7 features.
Official documentation: https://tencent.github.io/wepy/
Installation and Project Setup
Install the CLI:
npm install -g wepy-cliCreate a demo project:
wepy init standard my-projectEnter the project folder, install dependencies (
npm install) and start the development server (
npm run dev).
Open the WeChat Developer Tools, point the project to the
distfolder, and disable the three default checks in the settings.
The demo runs successfully; the
distfolder contains the native mini‑program files.
Do not edit files directly in
dist. Instead, modify source files under
srcin your editor; the WeChat tool is only for preview.
.wpy File Structure
A .wpy file consists of three sections:
Style :
<style>...</style>(maps to wxss).
Template :
<template>...</template>(maps to wxml).
Script :
<script>...</script>(maps to js).
Demo Walkthrough
Data binding uses setData in native mini‑programs. When updating data inside async functions, you must manually call $apply to trigger dirty‑checking.
Component usage includes third‑party components (e.g., wepy-com-toast ) and communication methods $invoke and $broadcast . Mixins allow reusable logic; note that event execution order differs from Vue (component methods run before mixin methods in WePY).
The framework also supports concurrent requests; testing ten simultaneous requests shows WePY can handle more than the native limit of five.
Project Directory
Configuration File (wepy.config.js)
The root
wepy.config.jsdefines build options. Key fields include:
wpyExt : default
.wpy; change to
.vuefor IDE highlighting.
compilers : supports
wepy-compiler-less,
wepy-compiler-postcss,
wepy-compiler-sass,
wepy-compiler-babel,
wepy-compiler-pug(available from version 1.3.1).
Compilation Process
App Entry (app.wpy)
Extends
wepy.app, contains a
configproperty that generates
app.jsonduring compilation.
Page Entry (page.wpy)
Extends
wepy.page. Important properties:
Property
Description
config
Page config, equivalent to
index.json.
components
List of components used by the page.
data
Data rendered by the page.
methods
Event handlers such as
bindtap,
bindchange.
events
Inter‑component communication via
broadcastand
emit.
others
Lifecycle hooks like
onLoad,
onReady, and custom methods.
Component (com.wpy)
Extends
wepy.component; shares most page properties but without
configand page‑specific lifecycle events.
Using a component involves three steps, illustrated below:
Component Communication
Components communicate via
$invoke,
$broadcast, and event listeners, enabling isolated and reusable logic across the mini‑program.
— End of guide —
MaoDou Frontend Team
Open-source, innovative, collaborative, win‑win – sharing frontend tech and shaping its future.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.