微信小程序实战(一)---实现登录界面

(本文转自杨小事er,原博客地址:http://blog.csdn.net/qq_25936689/article/details/53044178)

昨天小程序第一天公测,就下载个小程序自带IDE玩了玩,看了看API,撸出了个登录界面给大家分享下。

下面是主界面和代码。


index.wxml

  1. <view class="container">  
  2.   <view class="usermotto">  
  3.     <text class="user-motto">{{motto}}</text>  
  4.     用户名:  
  5.     <input type="text" bindinput="userNameInput"/>  
  6.     密码:  
  7.     <input type="text" bindinput="userPasswordInput" password="true"/>  
  8.     <button bindtap="logIn">登录</button>  
  9.   </view>  
  10. </view>  
index.js

  1. var app = getApp()  
  2. Page({  
  3.   data: {  
  4.     motto: '欢迎登录WXapp',  
  5.     userName:'',  
  6.     userPassword:'',  
  7.     id_token:'',//方便存在本地的locakStorage  
  8.     response:'' //存取返回数据  
  9.   },  
  10.   userNameInput:function(e){  
  11.     this.setData({  
  12.       userName: e.detail.value  
  13.     })  
  14.   },  
  15.   userPasswordInput:function(e){  
  16.     this.setData({  
  17.       userPassword: e.detail.value  
  18.     })  
  19.     console.log(e.detail.value)  
  20.   },  
  21.   logIn:function(){  
  22.     var that = this  
  23.     wx.request({  
  24.       url: 'http://localhost:8000/admin',  
  25.       data: {  
  26.         username: this.data.userName,  
  27.         password: this.data.userPassword,  
  28.       },  
  29.       method: 'GET',  
  30.       success: function (res) {  
  31.         that.setData({  
  32.           id_token: res.data.id_token,  
  33.           response:res  
  34.         })  
  35.         try {  
  36.           wx.setStorageSync('id_token', res.data.id_token)  
  37.         } catch (e) {  
  38.         }  
  39.         wx.navigateTo({  
  40.           url: '../components/welcome/welcome'  
  41.         })  
  42.         console.log(res.data);  
  43.       },  
  44.       fail: function (res) {  
  45.         console.log(res.data);  
  46.         console.log('is failed')  
  47.       }  
  48.     })  
  49.   }  
  50. })  

源码都放在这里了,https://github.com/Yangzhedi/myBlog-wxapp,欢迎star,issue呦~

代码就是以上那些,个人感觉小程序和react真的很像,不亏于小程序源码中imoort react的这一句。所以有react基础的会更好上手小程序的吧~


js文件里Page里的data就类似与React中的state的机制,

之后在js文件中想要调用data里的数据就必须才才用this.data.XXX;

但是在wxml中想要绑定data里的数据,就才用双括号的方法,而且!不需要!this.data。直接就是{{XXX}}。


在回到代码里看,wxml中主要就是两个input框和一个button。通过小程序input的原生API - bindInput (文档:小程序input),就可以获取input的值,

然后在data里定义两个(userName和userPassword)来存取这两个input的输入值。

再通过button的 bindTap绑定js文件中的logIn函数。(文档:小程序button


在logIn函数中,就通过this.data.userName和this.data.userPassword来获取曾经输入的两个值。

在通过调用微信发送请求API,把两个值放在请求中,这就有点像ajax发送请求了。

再在success中写下成功之后想要做的事情,比如这个例子里,就跳转到welcom页面。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值