學(xué)習(xí)兩周左右微信小程序了,做個(gè)小結(jié)吧,以后有時(shí)間再學(xué)了。玩玩還是挺有意思的。以下是小結(jié):
傳遞參數(shù)的3種方式(注意:以下傳遞的參數(shù)名都要使用小寫字母,不要使用駝峰命名法或任何有大寫字母的命名方式,不然傳遞過去都會(huì)被強(qiáng)轉(zhuǎn)成小寫字母,導(dǎo)致獲取的時(shí)候出錯(cuò)!!):
1、形式如 data-variable="{{variable}}",(variable代表變量名):
.wxml設(shè)置傳遞的參數(shù):variable,格式如下:
-
[html] view plain copy print?
-
<view bindtap="functionName" data-variable="{{variable}}">
.js接收傳遞過來的參數(shù):event.currentTarget.dataset.variable;(注意是currentTarget而不是target,直接console.log(event);具體可在控制臺(tái)查看event存放的數(shù)據(jù)結(jié)構(gòu)再?zèng)Q定訪問方式。)
-
[javascript] view plain copy print?
-
functionName:function(event){
-
console.log(event);
-
var variableData = event.currentTarget.dataset.variable;
-
}
2、形式如 variable="{{variable}}",(variable代表變量名):(此方式經(jīng)測(cè)試貌似有局限性,只能傳遞id過去,其它變量名無法接收),接收id:event.currentTarget.id或event.target.id。 hotMovies.wxml文件:
-
[html] view plain copy print?
-
<view wx:for="{{hotMovies(你的內(nèi)容)}}" wx:for-item="item">
-
<view class="picView">
-
<image class="pic" src="{{item.images.medium(你的圖片路徑)}}" id="{{item.id(每一項(xiàng)的id)}}" bindtap="toDetail(綁定的點(diǎn)擊函數(shù))" />
-
</view>
-
</view>
-
hotMovies.js文件:
-
[javascript] view plain copy print?
-
toDetail: function (event) {//參數(shù):事件對(duì)象
-
console.log(event);
-
wx.navigateTo({
-
url: '/pages/detail/detail?id=' + event.currentTarget.id,/**url傳參*/
-
})
-
}
3、繼續(xù)接著2的例子看,這個(gè)主要是 url 傳參: hotMovies.js文件:
-
[javascript] view plain copy print?
-
toDetail: function (event) {//參數(shù):事件對(duì)象
-
console.log(event);
-
wx.navigateTo({
-
url: '/pages/detail/detail?id=' + event.currentTarget.id,/**url傳參*/
-
})
-
}
detail.js文件:
-
[javascript] view plain copy print?
-
onLoad: function (options) {
-
/**括號(hào)內(nèi)的options可以改名的,結(jié)果一樣,無影響,已測(cè)試!*/
-
console.log("options:");
-
-
console.log(options);//就是一個(gè)接收傳遞過來的參數(shù)的對(duì)象
-
var filmId = options.id;(接受url傳參,不限制只能傳遞id變量名,可以傳遞多個(gè)變量名值)
-
console.log(filmId);//獲取在首頁(yè)點(diǎn)擊的電影圖片的id
-
/**具體邏輯實(shí)現(xiàn) */
-
}
設(shè)置一個(gè)input輸入要搜索的內(nèi)容加上一個(gè)按鈕點(diǎn)擊即搜索(簡(jiǎn)單例子): .wxml文件
-
[html] view plain copy print?
-
<input bindinput="keyword" placeholder="默認(rèn)文字的內(nèi)容" placeholder-style="默認(rèn)文字的樣式"/>
-
<button bindtap="requestSomething" data-keyword="{{keyword}}">搜索</button><!--data-keyword
-
-
向函數(shù)傳參keyword-->
.js文件
-
[javascript] view plain copy print?
-
keyword:function(event){
-
this.setData({
-
keyword:event.detail.value /**獲取input輸入的值并設(shè)置到搜索值 */
-
});
-
}
-
requestSomething: function (event) {
-
var keyword = null;
-
if(event){//點(diǎn)擊了搜索按鈕才有此值
-
keyword = event.currentTarget.dataset.keyword;/**獲取到值后再請(qǐng)求相關(guān)數(shù)據(jù) */
-
}
-
/**此處根據(jù)需要請(qǐng)求相關(guān)api獲取數(shù)據(jù) */
-
}
微信小程序本地存儲(chǔ)數(shù)據(jù)與讀取數(shù)據(jù): 存儲(chǔ)數(shù)據(jù):
-
[javascript] view plain copy print?
-
wx.setStorage({
-
//不會(huì)覆蓋原來key的內(nèi)容!
-
key: 'detailInfo'+id,
-
-
data: data,
-
-
})
或wx.setStorageSync('key', data); //會(huì)覆蓋原來key的內(nèi)容來存儲(chǔ)新的內(nèi)容!
讀取數(shù)據(jù):
-
[javascript] view plain copy print?
-
var storageInfo = wx.getStorageSync(key);/**獲取本地同步數(shù)據(jù) */
微信小程序的發(fā)請(qǐng)求request:
-
[javascript] view plain copy print?
-
var data={...params...};//傳遞的參數(shù)對(duì)象
-
wx.request({
-
url: url, //請(qǐng)求api的接口地址
-
data: data,//傳遞的參數(shù)
-
header: {
-
'content-type': 'json'//不能寫"application/json"否則報(bào)400錯(cuò)誤。
-
},
-
success: function (res) {//請(qǐng)求數(shù)據(jù)成功后才執(zhí)行的回調(diào)函數(shù)。
-
console.log(res);
-
that.setData({/**放在外部沒效果,因?yàn)檫€沒執(zhí)行成功就分配了數(shù)據(jù)結(jié)果是空數(shù)據(jù) */
-
key: value
-
});
-
wx.setStorageSync('key', data);//第一次請(qǐng)求之后存儲(chǔ)數(shù)據(jù)在本地
-
}
-
});
|