微信小程序 框架詳解及實(shí)例應(yīng)用
快速了解微信小程序的使用,一個(gè)根據(jù)小程序的框架開(kāi)發(fā)的todos app
微信官方已經(jīng)開(kāi)放微信小程序的官方文檔和開(kāi)發(fā)者工具。前兩天都是在看相關(guān)的新聞來(lái)了解小程序該如何開(kāi)發(fā),這兩天官方的文檔出來(lái)之后,趕緊翻看了幾眼,重點(diǎn)了解了一下文檔中框架與組件這兩個(gè)部分,然后根據(jù)簡(jiǎn)易教程,做了一個(gè)常規(guī)的todo app。這個(gè)app基于微信小程序的平臺(tái),實(shí)現(xiàn)了todo app的常規(guī)功能,同時(shí)為了讓它更接近實(shí)際的工作場(chǎng)景,也用到了loading與toast這兩個(gè)組件來(lái)完成一些操作的交互與反饋。這個(gè)平臺(tái)給我的直觀感受是,技術(shù)層面,它跟vue有相似性,但是遠(yuǎn)沒(méi)有vue強(qiáng)大;開(kāi)發(fā)時(shí)候的思路,不像vue,反倒覺(jué)得比較像backbone。所以要是使用過(guò)backbone,vue等mvc,mvvm框架的人,會(huì)覺(jué)得這個(gè)平臺(tái)上手很容易。本文主要介紹這個(gè)todo app實(shí)現(xiàn)的一些要點(diǎn)。
先補(bǔ)充下本文相關(guān)的資料:
官方文檔:https://mp.weixin.qq.com/debug/wxadoc/dev/index.html
官方開(kāi)發(fā)者工具下載:https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html
本文todo app的功能演示:

注:需長(zhǎng)按todo的text,才能直接編輯。因?yàn)槭窃谑謾C(jī)端,所以不能使用雙擊事件來(lái)進(jìn)行編輯,改成了長(zhǎng)按事件。小程序的平臺(tái)也沒(méi)有提供雙擊事件的綁定。
相關(guān)源碼:https://github.com/liuyunzhuge/blog/tree/master/todos/wx
如果你想在本地運(yùn)行這個(gè)項(xiàng)目,需要先安裝開(kāi)發(fā)者工具,按照文檔中簡(jiǎn)易教程的描述,先建好一個(gè)項(xiàng)目;
建完之后,開(kāi)發(fā)者工具就會(huì)打開(kāi)這個(gè)項(xiàng)目;
接著在磁盤(pán)上,找到建好的項(xiàng)目的文件夾,把里面的內(nèi)容都刪掉,把上面源碼文件夾下的文件都粘貼進(jìn)去;
然后重新打開(kāi)開(kāi)發(fā)者工具,先進(jìn)入到編輯頁(yè)簽,然后點(diǎn)擊編譯按鈕,就會(huì)直接進(jìn)入到調(diào)試界面,查看app的功能:

下面來(lái)介紹下這個(gè)app開(kāi)發(fā)的要點(diǎn):
1. 這個(gè)app的目錄結(jié)構(gòu)以及配置等就不詳細(xì)介紹了,這些在文檔-框架部分都有很詳細(xì)的描述。這個(gè)平臺(tái)里面沒(méi)有html和css,取而代之的是wxml和wxss。wxss跟css幾乎沒(méi)有區(qū)別,缺點(diǎn)就是不如css強(qiáng)大,支持的選擇器有限。但是好處是由于只有微信這一個(gè)平臺(tái),所以幾乎沒(méi)有兼容性問(wèn)題,能夠使用標(biāo)準(zhǔn)的,更新的css技術(shù)。wxml里面只能用平臺(tái)提供的那些組件的標(biāo)簽,html的標(biāo)簽不能直接用,各個(gè)組件的在wxml的使用方式,都可以在文檔-組件這一部分找到說(shuō)明的示例。所以實(shí)際上wxml跟wxss編寫(xiě)起來(lái)都沒(méi)有什么難題。
2. wxml支持以下這些特性:

在todo app里面除了模板和引用沒(méi)有用到之外,其它的都使用到了,不過(guò)沒(méi)有使用到每個(gè)特性的各個(gè)細(xì)節(jié),只根據(jù)app的需要選用合適的功能。前幾天看到有文章說(shuō),微信小程序可能是基于vue框架來(lái)實(shí)現(xiàn)的,所以就看了下vue的文檔。對(duì)于數(shù)據(jù)綁定,條件渲染,列表渲染,事件這幾部分都詳細(xì)看了vue的用法。對(duì)比下來(lái),wxml提供的這些特性,跟vue的相關(guān)特性是還比較像,不過(guò)功能并沒(méi)有那么多,所以也不能輕易地直接拿vue框架的特性用到小程序里面。最佳實(shí)踐,還是基于官方文檔中提供的說(shuō)明來(lái),如果官方文檔中沒(méi)有提到的功能,通過(guò)猜測(cè)的方式去用,肯定是行不通的。我通過(guò)打印的方式,查看一些對(duì)象的原型,也并沒(méi)有發(fā)現(xiàn)比官方文檔要多的一些實(shí)例方法,說(shuō)明小程序的框架功能確實(shí)是有限的。
3. wxss其實(shí)是可以用less或者sass來(lái)寫(xiě)的,只要選擇器滿(mǎn)足框架的要求即可。由于時(shí)間原因,就沒(méi)有在這個(gè)app里面去嘗試了。
4. 沒(méi)有雙向綁定。在vue里面,一個(gè)vue實(shí)例就是一個(gè)view-model;view層對(duì)數(shù)據(jù)的更新,會(huì)實(shí)時(shí)反饋到model;model的更新,也會(huì)實(shí)時(shí)反饋的到view。在小程序里面,沒(méi)有雙向綁定,view的更新不會(huì)直接同步到model;需要在相關(guān)事件回調(diào)里面,直接從view層拿到數(shù)據(jù),然后通過(guò)setData的方式,更新model,小程序內(nèi)部會(huì)在setData之后重新渲染page。比如單個(gè)todo項(xiàng),toggle的操作:
toggleTodo: function( e ) {
var id = this.getTodoId( e, 'todo-item-chk-' );
var value = e.detail.value[ 0 ];
var complete = !!value;
var todo = this.getTodo( id );
todo.complete = complete;
this.updateData( true );
this.updateStorage();
},
以上代碼中,通過(guò)e.detail.value[0]拿到單個(gè)todo項(xiàng)里面checkbox的值,通過(guò)該值來(lái)判斷todo的complete狀態(tài)。最后在updateData的內(nèi)部,還會(huì)通過(guò)setData方法,刷新model的內(nèi)容。只有這樣,在toggle操作之后,app底部的統(tǒng)計(jì)信息才會(huì)更新。
5. 事件綁定的時(shí)候,無(wú)法傳遞參數(shù),只能傳遞一個(gè)event。比如上面那個(gè)toggle的操作,我其實(shí)很想在回調(diào)里面把當(dāng)前todo的id傳到這個(gè)回調(diào)里面,但是想盡辦法都做不到,最后只能通過(guò)id的方式來(lái)處理:就是在wxml中綁定事件的組件上面,加一個(gè)id,這個(gè)id全page也不能重復(fù),所以id得加前綴,然后在id最后加上todo的id值;當(dāng)事件觸發(fā)的時(shí)候,通過(guò)e.currentTarget.id就能拿到該組件的id,去掉相應(yīng)的id前綴,就得到todo的id值了。這是目前用到的一個(gè)方法,我認(rèn)為不是很優(yōu)雅,希望后面能發(fā)現(xiàn)更好的辦法來(lái)實(shí)現(xiàn)。

6. app中考慮到了loading的效果,要利用button組件的loading屬性來(lái)實(shí)現(xiàn)。但是loading僅僅是一個(gè)樣式的控制,它不會(huì)控制這個(gè)按鈕是否能重復(fù)點(diǎn)擊。所以還要利用buttong的disabled屬性,防止重復(fù)點(diǎn)擊。
剩下的實(shí)現(xiàn)細(xì)節(jié),都在下面兩個(gè)文件的源碼中,歡迎大家指出其中的問(wèn)題。
index.wxml的源碼:
<!--list.wxml-->
<view class="container">
<view class="app-hd">
<view class="fx1">
<input class="new-todo-input" value="{{newTodoText}}" auto-focus bindinput="newTodoTextInput"/>
</view>
<button type="primary" size="mini" bindtap="addOne" loading="{{addOneLoading}}" disabled="{{addOneLoading}}">
+ Add
</button>
</view>
<view class="todos-list" >
<view class="todo-item {{index == 0 ? '' : 'todo-item-not-first'}} {{todo.complete ? 'todo-item-complete' : ''}}" wx:for="{{todos}}" wx:for-item="todo">
<view wx-if="{{!todo.editing}}">
<checkbox-group id="todo-item-chk-{{todo.id}}" bindchange="toggleTodo">
<label class="checkbox">
<checkbox value="1" checked="{{todo.complete}}"/>
</label>
</checkbox-group>
</view>
<view id="todo-item-txt-{{todo.id}}" class="todo-text" wx-if="{{!todo.editing}}" bindlongtap="startEdit">
<text>{{todo.text}}</text>
</view>
<view wx-if="{{!todo.editing}}">
<button id="btn-del-item-{{todo.id}}" bindtap="clearSingle" type="warn" size="mini" loading="{{todo.loading}}" disabled="{{todo.loading}}">
Clear
</button>
</view>
<input id="todo-item-edit-{{todo.id}}" class="todo-text-input" value="{{todo.text}}" auto-focus bindblur="endEditTodo" wx-if="{{todo.editing}}"/>
</view>
</view>
<view class="app-ft" wx:if="{{todos.length > 0}}">
<view class="fx1">
<checkbox-group bindchange="toggleAll">
<label class="checkbox">
<checkbox value="1" checked="{{todosOfUncomplted.length == 0}}"/>
</label>
</checkbox-group>
<text>{{todosOfUncomplted.length}} left.</text>
</view>
<view wx:if="{{todosOfComplted.length > 0}}">
<button type="warn" size="mini" bindtap="clearAll" loading="{{clearAllLoading}}" disabled="{{clearAllLoading}}">
Clear {{todosOfComplted.length}} of done.
</button>
</view>
</view>
<loading hidden="{{loadingHidden}}" bindchange="loadingChange">
{{loadingText}}
</loading>
<toast hidden="{{toastHidden}}" bindchange="toastChange">
{{toastText}}
</toast>
</view>
index.js的源碼:
var app = getApp();
Page( {
data: {
todos: [],
todosOfUncomplted: [],
todosOfComplted: [],
newTodoText: '',
addOneLoading: false,
loadingHidden: true,
loadingText: '',
toastHidden: true,
toastText: '',
clearAllLoading: false
},
updateData: function( resetTodos ) {
var data = {};
if( resetTodos ) {
data.todos = this.data.todos;
}
data.todosOfUncomplted = this.data.todos.filter( function( t ) {
return !t.complete;
});
data.todosOfComplted = this.data.todos.filter( function( t ) {
return t.complete;
});
this.setData( data );
},
updateStorage: function() {
var storage = [];
this.data.todos.forEach( function( t ) {
storage.push( {
id: t.id,
text: t.text,
complete: t.complete
})
});
wx.setStorageSync( 'todos', storage );
},
onLoad: function() {
this.setData( {
todos: wx.getStorageSync( 'todos' ) || []
});
this.updateData( false );
},
getTodo: function( id ) {
return this.data.todos.filter( function( t ) {
return id == t.id;
})[ 0 ];
},
getTodoId: function( e, prefix ) {
return e.currentTarget.id.substring( prefix.length );
},
toggleTodo: function( e ) {
var id = this.getTodoId( e, 'todo-item-chk-' );
var value = e.detail.value[ 0 ];
var complete = !!value;
var todo = this.getTodo( id );
todo.complete = complete;
this.updateData( true );
this.updateStorage();
},
toggleAll: function( e ) {
var value = e.detail.value[ 0 ];
var complete = !!value;
this.data.todos.forEach( function( t ) {
t.complete = complete;
});
this.updateData( true );
this.updateStorage();
},
clearTodo: function( id ) {
var targetIndex;
this.data.todos.forEach( function( t, i ) {
if( targetIndex !== undefined ) return;
if( t.id == id ) {
targetIndex = i;
}
});
this.data.todos.splice( targetIndex, 1 );
},
clearSingle: function( e ) {
var id = this.getTodoId( e, 'btn-del-item-' );
var todo = this.getTodo( id );
todo.loading = true;
this.updateData( true );
var that = this;
setTimeout( function() {
that.clearTodo( id );
that.updateData( true );
that.updateStorage();
}, 500 );
},
clearAll: function() {
this.setData( {
clearAllLoading: true
});
var that = this;
setTimeout( function() {
that.data.todosOfComplted.forEach( function( t ) {
that.clearTodo( t.id );
});
that.setData( {
clearAllLoading: false
});
that.updateData( true );
that.updateStorage();
that.setData( {
toastHidden: false,
toastText: 'Success'
});
}, 500 );
},
startEdit: function( e ) {
var id = this.getTodoId( e, 'todo-item-txt-' );
var todo = this.getTodo( id );
todo.editing = true;
this.updateData( true );
this.updateStorage();
},
newTodoTextInput: function( e ) {
this.setData( {
newTodoText: e.detail.value
});
},
endEditTodo: function( e ) {
var id = this.getTodoId( e, 'todo-item-edit-' );
var todo = this.getTodo( id );
todo.editing = false;
todo.text = e.detail.value;
this.updateData( true );
this.updateStorage();
},
addOne: function( e ) {
if( !this.data.newTodoText ) return;
this.setData( {
addOneLoading: true
});
//open loading
this.setData( {
loadingHidden: false,
loadingText: 'Waiting...'
});
var that = this;
setTimeout( function() {
//close loading and toggle button loading status
that.setData( {
loadingHidden: true,
addOneLoading: false,
loadingText: ''
});
that.data.todos.push( {
id: app.getId(),
text: that.data.newTodoText,
compelte: false
});
that.setData( {
newTodoText: ''
});
that.updateData( true );
that.updateStorage();
}, 500 );
},
loadingChange: function() {
this.setData( {
loadingHidden: true,
loadingText: ''
});
},
toastChange: function() {
this.setData( {
toastHidden: true,
toastText: ''
});
}
});
最后需要補(bǔ)充的是,這個(gè)app在有限的時(shí)間內(nèi)依據(jù)微信的官方文檔進(jìn)行開(kāi)發(fā),所以這里面的實(shí)現(xiàn)方式到底是不是合理的,我也不清楚。我也僅僅是通過(guò)這個(gè)app來(lái)了解小程序這個(gè)平臺(tái)的用法。希望微信官方能夠推出一些更全面、最好是項(xiàng)目性的demo,在代碼層面,給我們這些開(kāi)發(fā)者提供一個(gè)最佳實(shí)踐規(guī)范。歡迎有其它的開(kāi)發(fā)思路的朋友,幫我指出我以上實(shí)現(xiàn)中的問(wèn)題。
通過(guò)此文,希望大家了解微信小程序的框架,謝謝大家對(duì)本站的支持!
- 微信小程序 swiper組件輪播圖詳解及實(shí)例
- 微信小程序(應(yīng)用號(hào))簡(jiǎn)單實(shí)例應(yīng)用及實(shí)例詳解
- 微信小程序 實(shí)戰(zhàn)小程序?qū)嵗?/a>
- 微信小程序 for 循環(huán)詳解
- 微信小程序 參數(shù)傳遞詳解
- 微信小程序 (十七)input 組件詳細(xì)介紹
- 微信小程序 實(shí)現(xiàn)列表刷新的實(shí)例詳解
- 微信小程序 (三)tabBar底部導(dǎo)航詳細(xì)介紹
- 微信小程序 獲取微信OpenId詳解及實(shí)例代碼
- 微信小程序設(shè)置http請(qǐng)求的步驟詳解
- 微信小程序入門(mén)教程
- 微信小程序 頁(yè)面跳轉(zhuǎn)傳參詳解
- 微信小程序 輪播圖swiper詳解及實(shí)例(源碼下載)
相關(guān)文章
await 錯(cuò)誤捕獲實(shí)現(xiàn)方式源碼解析
這篇文章主要為大家介紹了await 錯(cuò)誤捕獲實(shí)現(xiàn)方式源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Umi4集成阿里低代碼框架lowcode-engine實(shí)現(xiàn)
這篇文章主要為大家介紹了Umi4集成阿里低代碼框架lowcode-engine實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
JavaScript前端實(shí)用的工具函數(shù)封裝
這篇文章主要為大家介紹了JavaScript前端實(shí)用的一些工具函數(shù)的封裝,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
用javascript制作qq注冊(cè)動(dòng)態(tài)頁(yè)面
這篇文章主要介紹了用javascript制作qq注冊(cè)動(dòng)態(tài)頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-04-04
Wireshark基本介紹和學(xué)習(xí)TCP三次握手
本文主要介紹Wireshark基本介紹和學(xué)習(xí)TCP三次握手,這里詳細(xì)整理了相關(guān)資料,并給出詳細(xì)流程,有需要的小伙伴可以參考下2016-08-08
electron創(chuàng)建新窗口模態(tài)框并實(shí)現(xiàn)主進(jìn)程傳值給子進(jìn)程
這篇文章主要為大家介紹了electron創(chuàng)建新窗口模態(tài)框并實(shí)現(xiàn)主進(jìn)程傳值給子進(jìn)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

