項目地址: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 如果你遇到一個錯誤,讓拷貝 wn.js 文件,請按提示信息將 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 然后在微信開發者工具中打開 dist/ 文件夾,就可以預覽開發了,可以選擇你喜歡的編輯器。 API注冊小程序創建 app.jsx 文件,這也是小程序的入口文件,可能像下面這樣寫 // 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 。 /* 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> ) } }
/* 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); } // ...
以上
|