1. 程式人生 > >wn-cli 像React組件開發一樣來開發微信小程序

wn-cli 像React組件開發一樣來開發微信小程序

window end 構建 const .com 編輯器 serve app.js align

項目地址:wn-cli

wn-cli

wn-cli 像React組件開發一樣來開發微信小程序

名字由來:wn -> weapp native 取第一個字母

Install

npm install wn-cli --save-dev
// 或者
yarn add wn-cli --dev

Usage

// 構建
npx wn ./examples ./dist

// 監聽模式
npx wn ./examples ./dist -w

如果你遇到一個錯誤,讓拷貝 wn.js 文件,請按提示信息將 node_modules 中的 node_modules/wn-cli/dist/wn.js 文件拷貝到 modules 文件夾中

你的目錄可能是這樣的:

├── dist
│   ├── app.js
│   ├── app.json
│   ├── app.wxss
│   ├── modules
│   │   └── wn.js
│   ├── pages
│   │   ├── index
│   │   │   ├── index.js
│   │   │   ├── index.json
│   │   │   └── index.wxml
│   │   │   └── index.wxss
│   │   └── me
│   │       ├── me.js
│   │       ├── me.json
│   │       └── me.wxml
│   │       └── me.wxss
│   └── project.config.json
├── package.json
├── project.config.json
├── src
│   ├── app.jsx
│   ├── app.css
│   └── pages
│       ├── index
│       │   ├── index.css
│       │   └── index.jsx
│       └── me
│       │   ├── me.css
│           └── me.jsx
└── yarn.lock

然後在微信開發者工具中打開 dist/ 文件夾,就可以預覽開發了,可以選擇你喜歡的編輯器。

API

註冊小程序

創建 app.jsx 文件,這也是小程序的入口文件,可能像下面這樣寫

// src/app.jsx
import { App } from ‘wn‘;
// 引入所有的頁面,相對路徑
import ‘./pages/index/index.jsx‘;
import ‘./pages/me/me.jsx‘;

export default class extends App {
  debug = true

  window = {
    navigationBarTitleText: ‘hello‘,
    navigationBarTextStyle: ‘black‘,
    navigationBarBackgroundColor: ‘#f4f5f6‘,
    backgroundColor: ‘#f4f5f6‘,
  }

  tabBar = {
    color: ‘#333333‘,
    backgroundColor: ‘#ffffff‘,
    list: [
      {
        pagePath: ‘pages/index/index‘, // 編譯後js路徑
        text: ‘首頁‘,
      },
      {
        pagePath: ‘pages/me/me‘,
        text: ‘我‘,
      },
    ],
  }

  myData = ‘自定義公共變量‘

  hello() { return ‘自定義公共函數‘ }

  // 生命周期函數
  onLaunch() { console.log(‘app: hello onLaunch‘) }
  onShow() { console.log(‘app: hello onShow‘) }
  onHide() { console.log(‘app: hello onHide‘) }
  onError() { console.log(‘app: hello onError‘) }
}

同樣的,可以通過在 app.js 同目錄下創建 app.css ,來書寫公用的 css

/* src/app.css */
.test {
  color: red;
}

如此,小程序就註冊好了。

創建頁面

創建第一個頁面,在 src/pages 下面創建頁面文件,如 index/index.jsx,可以這樣寫:

// src/pages/index/index.jsx
import { Page, wx } from ‘wn‘

export default class extends Page {
  window = {
    navigationBarTitleText: ‘hello‘
  }
  navigationBarTextStyle = ‘black‘

  async onShow() {
    const systemInfo = await wx.getSystemInfo()
    console.log(‘系統信息‘, systemInfo);
  }

  data = {
    name: ‘小程序‘
  }

  render() {
    return (
      <view class="test">
        你好,{name}      
      </view>
    )
  }
}

添加文件作用域的樣式文件,相當於css module,在 src/pages/index 文件夾下面創建同名 css 文件 index.css,不用再導入,只需要命名和同文件下的 .jsx 文件相同就可以了

/* src/pages/index/index.css */
.test {
  color: blue;
  text-align: center;
}

如此第一個頁面就創建好了,接下來你可以添加自己的 me.jsx 頁面。

創建組件

創建第一個組件,如 header,在 src/components下面創建 header/header.jsxheader/header.css,兩文件

// src/components/header/header.jsx
import { Component } from ‘wn‘

export default class extends Component {
  render() {
    return (
      <view class="header">
        <slot></slot>  
      </view>
    )
  }
}
  • slot 表示組件的 children 放置的位置,還可以指定位置,設置 slotname
/* src/components/header/header.css */
.header {
  color: blue;
}

使用組件

創建了組件後,在頁面中使用,首先在頁面中導入:

import header from ‘../../components/header/header.jsx‘;

然後在需要的時候使用:

render() {
    return (
      <view class="test">
        <header>
          hello
        </header>
        你好,{name}      
      </view>
    )
  }

也可以組件嵌套等。

Promise 化微信 API,即使用 Promise 代理 wx 中的異步方法

如:

// ...
async onShow() {
    const systemInfo = await wx.getSystemInfo()
    console.log(systemInfo);
  }
// ...
  • 註:原生 API 配置中的 complete 方法並沒有代理

以上

  • 暫時的功能能滿足大多數簡單的微信小程序開發,後續在使用中遇到瓶頸了,再配置兼容性開發高級 API 滿足需求。
  • 最後的目的是能滿足所有(99%)微信小程序開發者的需求,全面(99%)覆蓋小程序開發。像 React Native 開發 APP 一樣,用wn-cli 開發 weapp (微信小程序)
  • 離目標還有不小的距離,如果你也是 React 派,對微信小程序有興趣,可以 fork 代碼共同建設維護這個工程 ,或許比較懶,那就直接提 ISSUE,這兩樣都會使我開心一整天的 => 項目地址:wn-cli。

wn-cli 像React組件開發一樣來開發微信小程序