
一:url带参数传递
与前端语言一样,小程序页面间的传递可以通过在路由url后接参数,路由的同时会将参数一并传递到新的页面。
indexwxml:
效果图:
二:将值存入全局变量
我们同样可以将需要的值存入全局变量中,在需要的地方直接引用就好了。
需要用到全局变量时记得要先获取应用实例: const app = getApp()
三, 用event传值
JS文件
JS代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//indexjs
//获取应用实例
var app = getApp()
Page({
data: {
motto: '示例小程序-获取当前地理位、速度',
userInfo: {},
hasLocation:false,
location:{}
},
//事件处理函数
bindViewTap: function() {
wxnavigateTo({
url: '/logs/logs'
})
},
onLoad: function () {
consolelog('onLoad')
var that = this
//调用应用实例的方法获取全局数据
appgetUserInfo(function(userInfo){
//更新数据
thatsetData({
userInfo:userInfo
})
}),
wxgetLocation( {
success: function( res ) {
consolelog( res )
thatsetData( {
hasLocation: true,
location: {
longitude: reslongitude,
latitude: reslatitude
}
})
}
})
}
})
indexwxml
1
2
3
4
5
6
7
8
9
10
11
12
<!--indexwxml-->
<view class="container">
<view bindtap="bindViewTap" class="userinfo">
<image class="userinfo-avatar" src="{{userInfoavatarUrl}}" background-size="cover"></image>
<text class="userinfo-nickname">{{userInfonickName}}</text>
</view>
<view class="usermotto">
<!-- <text class="user-motto">{{motto}}\n</text>-->
<text>经度:{{locationlongitude}}\n</text>
<text>纬度:{{locationlatitude}}</text>
</view>
</view>
运行效果
微信小程序 获取javascript 里的数据 wxml如何获取js里的数据 例: wxml里: {{txt}} 通过上面的{{txt}}可以对应获取js里data下定义的txt的值 js里: data: { txt:{} } 首先在data里定义一个“容器”txt:{},{}内为空代表是从别的地方传值进去厦门资讯网
1、服务器获取到的数组中嵌套 对象组成的数组,控制台输出:
2、解析并绑定服务器获取到的数据,js文件:
3、绑定一层的shopList数据, wxml文件:
4、绑定shopList数组中的二层equips数据,wxml文件 中 只需要itemequips即可
最开始循环获取到数据后,总以为需要嵌套循环出二层数组对象,并分别绑定数据。兜兜转转绕了一大圈,发现只要将一级数组循环获取到,二层数组对象直接“itemxxx”就可以在wxml文件中直接进行数据绑定了。
做一个查询的功能 可以根据id查到text 或者根据text查到id
附上数组如下
resultset: [{id:1,text:"aaa"},{id:2,text:"bbb"}, {id: 3,text: 'ccc'}, {id: 4,text: 'f'}]
之前尝试了一些方法 就像在传统语言那样遍历数组然后寻找相等的值 但是发现好像并不是特别好使
var resultSet =thisdataresultset
const length = resultSetlength var val = thisdatainputValue if(val){
consolelog(val) for(let i =0;i<=length;i++){ //consolelog(resultSet[i])
if(val == resultSet[i]id){
consolelo("找到了") var result = thisdataresultSet[i]id
consolelog("结果是"+result)
} else{
consolelog("没找到结果")
}
}
} else{
consolelog("没输入啊")
}
}
以上就是关于微信小程序实现wxml视图向js传递参数的方法(点击view带参数进入js)全部的内容,包括:微信小程序实现wxml视图向js传递参数的方法(点击view带参数进入js)、微信小程序获取到位置坐标后,怎么获取具体地理位置信息、微信小程序中nd滤镜换算方法怎样获取等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)