小程序模板網(wǎng)

wn-cli 像React組件開(kāi)發(fā)一樣來(lái)開(kāi)發(fā)微信小程序

發(fā)布時(shí)間:2018-05-04 15:50 所屬欄目:小程序開(kāi)發(fā)教程

項(xiàng)目地址:wn-cli

wn-cli

wn-cli 像React組件開(kāi)發(fā)一樣來(lái)開(kāi)發(fā)微信小程序

名字由來(lái):wn -> weapp native 取第一個(gè)字母

Install

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

Usage

// 構(gòu)建
npx wn ./examples ./dist

// 監(jiān)聽(tīng)模式
npx wn ./examples ./dist -w

如果你遇到一個(gè)錯(cuò)誤,讓拷貝 wn.js 文件,請(qǐng)按提示信息將 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

然后在微信開(kāi)發(fā)者工具中打開(kāi) dist/ 文件夾,就可以預(yù)覽開(kāi)發(fā)了,可以選擇你喜歡的編輯器。

API

注冊(cè)小程序

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

// src/app.jsx
import { App } from 'wn';
// 引入所有的頁(yè)面,相對(duì)路徑
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: '首頁(yè)',
      },
      {
        pagePath: 'pages/me/me',
        text: '我',
      },
    ],
  }

  myData = '自定義公共變量'

  hello() { return '自定義公共函數(shù)' }

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

同樣的,可以通過(guò)在 app.js 同目錄下創(chuàng)建 app.css ,來(lái)書寫公用的 css 。

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

如此,小程序就注冊(cè)好了。

創(chuàng)建頁(yè)面

創(chuàng)建第一個(gè)頁(yè)面,在 src/pages 下面創(chuàng)建頁(yè)面文件,如 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('系統(tǒng)信息', systemInfo);
  }

  data = {
    name: '小程序'
  }

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

添加文件作用域的樣式文件,相當(dāng)于css module,在 src/pages/index 文件夾下面創(chuàng)建同名 css 文件 index.css,不用再導(dǎo)入,只需要命名和同文件下的 .jsx 文件相同就可以了

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

如此第一個(gè)頁(yè)面就創(chuàng)建好了,接下來(lái)你可以添加自己的 me.jsx 頁(yè)面。

創(chuàng)建組件

創(chuàng)建第一個(gè)組件,如 header,在 src/components下面創(chuàng)建 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 放置的位置,還可以指定位置,設(shè)置 slot 的 name。
/* src/components/header/header.css */
.header {
  color: blue;
}

使用組件

創(chuàng)建了組件后,在頁(yè)面中使用,首先在頁(yè)面中導(dǎo)入:

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

然后在需要的時(shí)候使用:

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 方法并沒(méi)有代理

以上

  • 暫時(shí)的功能能滿足大多數(shù)簡(jiǎn)單的微信小程序開(kāi)發(fā),后續(xù)在使用中遇到瓶頸了,再配置兼容性開(kāi)發(fā)高級(jí) API 滿足需求。
  • 最后的目的是能滿足所有(99%)微信小程序開(kāi)發(fā)者的需求,全面(99%)覆蓋小程序開(kāi)發(fā)。像 React Native 開(kāi)發(fā) APP 一樣,用wn-cli 開(kāi)發(fā) weapp (微信小程序)。
  • 離目標(biāo)還有不小的距離,如果你也是 React 派,對(duì)微信小程序有興趣,可以 fork 代碼共同建設(shè)


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開(kāi)源 碼云倉(cāng)庫(kù):starfork
本文地址:http://www.u-renovate.com/wxmini/doc/course/24228.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×