知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 小程序相關(guān) >
wn-cli 像React組件開發(fā)一樣來開發(fā)微信小程序
發(fā)表時(shí)間:2021-3-31
發(fā)布人:葵宇科技
瀏覽次數(shù):64
項(xiàng)目地址:wn-cli
wn-cli
wn-cli 像React組件開發(fā)一樣來開發(fā)微信小程序
名字由來: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)聽模式
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
然后在微信開發(fā)者工具中打開 dist/
文件夾,就可以預(yù)覽開發(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 = 'https://www.wxapp-union.com/自定義公共變量'
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') }
}
同樣的,可以通過在 app.js
同目錄下創(chuàng)建 app.css
,來書寫公用的 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 = https://www.wxapp-union.com/{
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)建好了,接下來你可以添加自己的 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
方法并沒有代理
以上
- 暫時(shí)的功能能滿足大多數(shù)簡(jiǎn)單的微信小程序開發(fā),后續(xù)在使用中遇到瓶頸了,再配置兼容性開發(fā)高級(jí) API 滿足需求。
- 最后的目的是能滿足所有(99%)微信小程序開發(fā)者的需求,全面(99%)覆蓋小程序開發(fā)。像
React Native
開發(fā)APP
一樣,用wn-cli
開發(fā)weapp (微信小程序)
。 - 離目標(biāo)還有不小的距離,如果你也是
React
派,對(duì)微信小程序有興趣,可以fork
代碼共同建設(shè)維護(hù)這個(gè)工程 ,或許比較懶,那就直接提ISSUE
,這兩樣都會(huì)使我開心一整天的 => 項(xiàng)目地址:wn-cli。
相關(guān)案例查看更多
相關(guān)閱讀
- 云南小程序定制
- 云南網(wǎng)絡(luò)營(yíng)銷
- 大理網(wǎng)站建設(shè)公司
- 云南建設(shè)廳官方網(wǎng)站
- 小程序模板開發(fā)公司
- 網(wǎng)站建設(shè)首選公司
- 云南科技公司
- 云南省建設(shè)廳網(wǎng)站
- 云南網(wǎng)絡(luò)營(yíng)銷顧問
- 云南企業(yè)網(wǎng)站
- 商標(biāo)
- 網(wǎng)站建設(shè)快速優(yōu)化
- 網(wǎng)站建設(shè)公司地址
- 小程序設(shè)計(jì)
- 云南小程序代建
- 微信小程序開發(fā)入門課程
- 汽車報(bào)廢管理系統(tǒng)
- 云南小程序開發(fā)哪家好
- 網(wǎng)站維護(hù)
- 軟件定制公司
- 云南建站公司
- 曲靖小程序開發(fā)
- 分銷系統(tǒng)
- 貴州小程序開發(fā)
- 網(wǎng)站建設(shè)首頁(yè)
- 云南網(wǎng)站建設(shè)靠譜公司
- 網(wǎng)站制作
- APP
- 小程序表單
- 網(wǎng)站建設(shè)