wn-cli 像React组件开发一样来开发微信小程序

前端之家收集整理的这篇文章主要介绍了wn-cli 像React组件开发一样来开发微信小程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

项目地址:wn-cli

wn-cli

wn-cli 像React组件开发一样来开发微信小程序

名字由来:wn -> weapp native 取第一个字母

Install

  1. npm install wn-cli --save-dev
  2. // 或者
  3. yarn add wn-cli --dev

Usage

  1. // 构建
  2. npx wn ./examples ./dist
  3.  
  4. // 监听模式
  5. npx wn ./examples ./dist -w

如果你遇到一个错误,让拷贝 wn.js 文件,请按提示信息将 node_modules 中的 node_modules/wn-cli/dist/wn.js 文件拷贝到 modules 文件夹中

你的目录可能是这样的:

  1. ├── dist
  2. ├── app.js
  3. ├── app.json
  4. ├── app.wxss
  5. ├── modules
  6. └── wn.js
  7. ├── pages
  8. ├── index
  9. ├── index.js
  10. ├── index.json
  11. └── index.wxml
  12. └── index.wxss
  13. └── me
  14. ├── me.js
  15. ├── me.json
  16. └── me.wxml
  17. └── me.wxss
  18. └── project.config.json
  19. ├── package.json
  20. ├── project.config.json
  21. ├── src
  22. ├── app.jsx
  23. ├── app.css
  24. └── pages
  25. ├── index
  26. ├── index.css
  27. └── index.jsx
  28. └── me
  29. ├── me.css
  30. └── me.jsx
  31. └── yarn.lock

然后在微信开发者工具中打开 dist/ 文件夹,就可以预览开发了,可以选择你喜欢的编辑器。

API

注册小程序

创建 app.jsx 文件,这也是小程序的入口文件,可能像下面这样写

  1. // src/app.jsx
  2. import { App } from 'wn';
  3. // 引入所有的页面,相对路径
  4. import './pages/index/index.jsx';
  5. import './pages/me/me.jsx';
  6.  
  7. export default class extends App {
  8. debug = true
  9.  
  10. window = {
  11. navigationBarTitleText: 'hello',navigationBarTextStyle: 'black',navigationBarBackgroundColor: '#f4f5f6',backgroundColor: '#f4f5f6',}
  12.  
  13. tabBar = {
  14. color: '#333333',backgroundColor: '#ffffff',list: [
  15. {
  16. pagePath: 'pages/index/index',// 编译后js路径
  17. text: '首页',},{
  18. pagePath: 'pages/me/me',text: '我',],}
  19.  
  20. myData = '自定义公共变量'
  21.  
  22. hello() { return '自定义公共函数' }
  23.  
  24. // 生命周期函数
  25. onLaunch() { console.log('app: hello onLaunch') }
  26. onShow() { console.log('app: hello onShow') }
  27. onHide() { console.log('app: hello onHide') }
  28. onError() { console.log('app: hello onError') }
  29. }

同样的,可以通过在 app.js 同目录下创建 app.css ,来书写公用的 css

  1. /* src/app.css */
  2. .test {
  3. color: red;
  4. }

如此,小程序注册好了。

创建页面

创建第一个页面,在 src/pages 下面创建页面文件,如 index/index.jsx,可以这样写:

  1. // src/pages/index/index.jsx
  2. import { Page,wx } from 'wn'
  3.  
  4. export default class extends Page {
  5. window = {
  6. navigationBarTitleText: 'hello'
  7. }
  8. navigationBarTextStyle = 'black'
  9.  
  10. async onShow() {
  11. const systemInfo = await wx.getSystemInfo()
  12. console.log('系统信息',systemInfo);
  13. }
  14.  
  15. data = {
  16. name: '小程序'
  17. }
  18.  
  19. render() {
  20. return (
  21. <view class="test">
  22. 你好,{name}
  23. </view>
  24. )
  25. }
  26. }

添加文件作用域的样式文件,相当于css module,在 src/pages/index 文件夹下面创建同名 css 文件 index.css,不用再导入,只需要命名和同文件下的 .jsx 文件相同就可以了

  1. /* src/pages/index/index.css */
  2. .test {
  3. color: blue;
  4. text-align: center;
  5. }

如此第一个页面就创建好了,接下来你可以添加自己的 me.jsx 页面

创建组件

创建第一个组件,如 header,在 src/components下面创建 header/header.jsxheader/header.css,两文件

  1. // src/components/header/header.jsx
  2. import { Component } from 'wn'
  3.  
  4. export default class extends Component {
  5. render() {
  6. return (
  7. <view class="header">
  8. <slot></slot>
  9. </view>
  10. )
  11. }
  12. }
  • slot 表示组件的 children 放置的位置,还可以指定位置,设置 slotname
  1. /* src/components/header/header.css */
  2. .header {
  3. color: blue;
  4. }

使用组件

创建了组件后,在页面中使用,首先在页面中导入:

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

然后在需要的时候使用:

  1. render() {
  2. return (
  3. <view class="test">
  4. <header>
  5. hello
  6. </header>
  7. 你好,{name}
  8. </view>
  9. )
  10. }

也可以组件嵌套等。

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

如:

  1. // ...
  2. async onShow() {
  3. const systemInfo = await wx.getSystemInfo()
  4. console.log(systemInfo);
  5. }
  6. // ...
  • 注:原生 API 配置中的 complete 方法并没有代理

以上

  • 暂时的功能能满足大多数简单的微信小程序开发,后续在使用中遇到瓶颈了,再配置兼容性开发高级 API 满足需求。
  • 最后的目的是能满足所有(99%)微信小程序开发者的需求,全面(99%)覆盖小程序开发。像 React Native 开发 APP 一样,用wn-cli 开发 weapp (微信小程序
  • 离目标还有不小的距离,如果你也是 React 派,对微信小程序有兴趣,可以 fork 代码共同建设维护这个工程 ,或许比较懒,那就直接提 ISSUE,这两样都会使我开心一整天的 => 项目地址:wn-cli

猜你在找的React相关文章