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 ,来书写公用的 cssjson

/* 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
相关文章
相关标签/搜索