在公司小程序开发也快7个月了,当然我是做java后端的,但是没办法做全栈的总要会写小程序js编写。刚来公司那会儿还没接触过小程序呢,今天就对小程序的各种取值问题进行归纳总结。希望帮到初学者!!!
当然其他有啥不懂的还是推荐大家去看官方api文档自学一下。写的还是挺全的。
https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.switchTab.html
1.在xxx.js文件中如下的data对象里面的所有属性表示的是本页面的全局变量,只能在这个js文件里调用,在其他js文件就无法调用了.

2.有了局部变量当然也少不了整个小程序的全局变量。在app.js文件中globalData对象里的所有属性在所有js文件里都可以调用

好了正文开始。
/**
*1.app.globalData.xxx ,获取全局变量中xxx属性的值
*2.onLoad()监听函数中通过function中的对象获取请求路径中参数
*3.that.data.xxx ,获取页面data对象中xxx属性的值
*/
wx.request({
url: app.globalData.preUrl + '/front/law/list', //在app.js 全局变量globalData对象中取值
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
type: typeValue,
index: that.data.index // 在页面初始数据data对象中取值
},
method: 'POST',
success: function(res) {
that.setData({ //给本页全局变量赋值
lists: res.data.laws, // 取出后台传来的json数据
url: app.globalData.preUrl
}, function() {
wx.hideLoading();
})
},
error: function() {
wx.hideLoading();
}
})
},
/**
* 利用bindinput事件 获取值
*/
//.wxml文件
<input class='centertxt' type='number' placeholder='请输入您的手机号' placeholder-class='placeholder' bindinput='getPhone'></input>
//.js文件
getPhone: function(e) {
var that = this;
that.setData({
applyPhone: e.detail.value //获取值并赋值
})
},
/**
*view标签 加 bindtap 绑定事件,用data-name传值(data-xxx 这个xxx命名必须全部小写)
*/
//.wxml文件
<view class="weui-cell" bindtap='toResult' data-contentpath='{{url+list.contentPath}}'>
// .js文件
toResult: function (event) {
var src = event.currentTarget.dataset.contentpath; //获取data-contentpath
}
/**
*通过绑定事件对应的对象获取value值
*/
//.wxml文件
<picker bindchange='pcikerChange' value="{{index}}" range="{{lawlist}}">
<view class="picker">
{{lawlist[index]}}
</view>
</picker>
//.js文件
pcikerChange: function(e) {
let that = this;
//console.log(e.detail.value);
that.setData({
index: e.detail.value //通过绑定事件对应的对象获取value值
})
},
本文分享了7个月小程序开发经验,涵盖全局与局部变量使用、页面数据交互、事件处理及API调用技巧,适合初学者快速上手。

3749

被折叠的 条评论
为什么被折叠?



