wn-cli 像React组件开发一样来开发微信小程序
项目地址:wn-cli wn-cliwn-cli 像React组件开发一样来开发微信小程序 名字由来:wn -> weapp native 取第一个字母 Installnpm install wn-cli --save-dev // 或者 yarn add wn-cli --dev Usage// 构建 npx wn ./examples ./dist // 监听模式 npx wn ./examples ./dist -w 如果你遇到一个错误,让拷贝 你的目录可能是这样的: ├── 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 然后在微信开发者工具中打开 API注册小程序创建 // 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') } } 同样的,可以通过在 /* src/app.css */ .test { color: red; } 如此,小程序就注册好了。 创建页面创建第一个页面,在 // 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> ) } } 添加文件作用域的样式文件,相当于 /* src/pages/index/index.css */ .test { color: blue; text-align: center; } 如此第一个页面就创建好了,接下来你可以添加自己的 创建组件创建第一个组件,如 // src/components/header/header.jsx import { Component } from 'wn' export default class extends Component { render() { return ( <view class="header"> <slot></slot> </view> ) } }
/* 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); } // ...
以上
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |