微信小程序笔记(初级)--页面间数据传输

发布于:2022-12-24 ⋅ 阅读:(363) ⋅ 点赞:(0)

1.index文件内
(1)index.js->index.wxml

// index.js
onLoad() {
    var x='hello'
    var y='world'
    var that=this
    that.setData({
      x:x,
      y:y
    })
  },
// index.wxml
<view>{{x}}<view>
<view>{{y}}<view>


(2)index.wxml->index.js

//index.js
hello(e){
    console.log(e)
    var x=e.currentTarget.dataset.x
    console.log(x)
  },
world(e){
    console.log(e)
    var y=e.currentTarget.dataset.y
    console.log(y)
  },
//index.wxml
<view bindtap="hello" data-x="{{x}}">{{x}}</view>
<view bindtap="world" data-y="{{y}}">{{y}}</view>

点击hello,控制台打印hello
点击world,控制台打印world

2.index文件外(index->log)
(1)index.js->log.js->log.wxml

//index.js
  tz(){
    var a=1
    var b=2
    wx.navigateTo({
      url: '../logs/logs?a='+a+'&b='+b,
    })
  },
//index.wxml
<button bindtap="tz">点击跳转到log页面</button>

//log.js
  onLoad: function (options) {
    var that=this
    var a=options.a
    var b=options.b
    console.log(a)
    console.log(b)
    that.setData({
      a:a,
      b:b
    })
  },
//log.wxml
<view>{{a}}</view>
<view>{{b}}</view>

点击“点击跳转到log页面”


网站公告

今日签到

点亮在社区的每一天
去签到