6.2 登录取得用户名
前面的登录弹出的框并不能和登录名和密码联系起来,例如“张三,登录成功”。这一节我们来解决这个问题
1。首先需要一个变量username,来表示用户名,把这个变量定义在data里,和motto位置相当,注意要在motto最后添加逗号。
Page({
data:{
motto:'hello world',
username:''
},
.....
})
2.在弹出框使用这个username变量
同一个页面使用data里面的变量,需要用下面的代码
this.data.username
那么弹出框的代码就变成了
wx.showToast({
title:this.data.username+',欢迎你'
})
这样弹出框就能使用username这个变量了。
3。账号输入框输入的时候,把输入框的值更新到username
输入框input有一个input事件,键盘输入的时候触发,然后我们可以绑定这个事件:
<inputbindinput='aaa'></input>
这个aaa就是input事件,在页面定义这个函数
Page({
data:{
motto:'hello world',
username:''
},
.....,
aaa:function(e){
},
...
})
e.detail.value就可以得到输入框当前的值。然后把这个值赋值给username就可以了,注意赋值一定要使用this.setData方法,例如下面就是设置本页面的变量x的值为abcdd
this.setData({
x:'abcdd'
})
直接赋值this.data.x='abcdd'是不可以的。