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

項目地址:wn-clijavascript

wn-cli

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

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

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 文件夾中node

你的目錄多是這樣的:git

├── 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/ 文件夾,就能夠預覽開發了,能夠選擇你喜歡的編輯器。github

API

註冊小程序

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

// 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 。json

/* 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.jsx 和 header/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 放置的位置,還能夠指定位置,設置 slot 的 name
/* 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
相關文章
相關標籤/搜索