
大多app都有歡迎界面,那么小程序開(kāi)發(fā)是否也可以設(shè)計(jì)一個(gè)登錄歡迎界面,下面為大家介紹小程序登錄界面樣式開(kāi)發(fā)實(shí)例:
index.wxml
<view class="container">
<view class="usermotto">
<text class="user-motto">{{motto}}</text>
用戶名:
<input type="text" bindinput="userNameInput"/>
密碼:
<input type="text" bindinput="userPasswordInput" password="true"/>
<button bindtap="logIn">登錄</button>
</view>
</view>
index.js
var app = getApp()
Page({
data: {
motto: '歡迎登錄WXapp',
userName:'',
userPassword:'',
id_token:'',//方便存在本地的locakStorage
response:'' //存取返回?cái)?shù)據(jù)
},
userNameInput:function(e){
this.setData({
userName: e.detail.value
})
},
userPasswordInput:function(e){
this.setData({
userPassword: e.detail.value
})
console.log(e.detail.value)
},
logIn:function(){
var that = this
wx.request({
url: 'http://localhost:8000/admin',
data: {
username: this.data.userName,
password: this.data.userPassword,
},
method: 'GET',
success: function (res) {
that.setData({
id_token: res.data.id_token,
response:res
})
try {
wx.setStorageSync('id_token', res.data.id_token)
} catch (e) {
}
wx.navigateTo({
url: '../components/welcome/welcome'
})
console.log(res.data);
},
fail: function (res) {
console.log(res.data);
console.log('is failed')
}
})
}
})
js文件里Page里的data就類似與react中的state的機(jī)制,
之后在js文件中想要調(diào)用data里的數(shù)據(jù)就必須才才用this.data.XXX;
但是在wxml中想要綁定data里的數(shù)據(jù),就才用雙括號(hào)的方法,而且!不需要!this.data。直接就是{{XXX}}。
在回到代碼里看,wxml中主要就是兩個(gè)input框和一個(gè)button。通過(guò)小程序input的原生API - bindInput (文檔:小程序input),就可以獲取input的值,
然后在data里定義兩個(gè)(userName和userPassword)來(lái)存取這兩個(gè)input的輸入值。
再通過(guò)button的 bindTap綁定js文件中的logIn函數(shù)。
在logIn函數(shù)中,就通過(guò)this.data.userName和this.data.userPassword來(lái)獲取曾經(jīng)輸入的兩個(gè)值。
在通過(guò)調(diào)用微信的發(fā)送請(qǐng)求API,把兩個(gè)值放在請(qǐng)求中,這就有點(diǎn)像ajax發(fā)送請(qǐng)求了。
再在success中寫(xiě)下成功之后想要做的事情,比如這個(gè)例子里,就跳轉(zhuǎn)到welcome頁(yè)面。
HiShop小程序工具提供多類型商城/門(mén)店小程序制作,可視化編輯 1秒生成5步上線。通過(guò)拖拽、拼接模塊布局小程序商城頁(yè)面,所看即所得,只需要美工就能做出精美商城。
更多小程序開(kāi)發(fā)案例,盡在:http://hydrodefense.cn/xiaocx/kaifa.html