微信小程序跨页面传值

微信小程序跨页面传值

'''
1. 获取当前页面

var pages = getCurrentPages();


2. //获取上一个页面

var prevPage = pages[pages.length - 2];  


3. 更新/赋值上一页面的值,并返回上一页面

getCurrentPages() 与 wx.navigateBack()结合使用
'''

发布页面/pages/publish/publish

1. // /pages/publish/publish.wxml

<navigator url="/pages/topic/topic" class="row" >
  <view class="left">{{topicTitle}}</view>
  <view class="right">
    <image class="go-icon" src='/static/images/icon/to_icon_show_small.png'></image>
  </view>
</navigator>


2. // pages/publish/publish.js

data: {
	topicId: null,
	topicTitle: "选择合适的话题",
},


updateTopic: function(item) {
	this.setData({
	  topicId: item.id,
	  topicTitle: item.title
	})
},

话题页面/pages/topic/topic(更新值:topicItem)

1. <!--pages/topic/topic.wxml-->
<view class="container">
  <view class="row" wx:for="{{topicList}}" data-item="{{item}}" bindtap="choseTopic">
  <text># {{item.title}}</text>
  <text>关注点 {{item.count}}</text>
  </view>
</view>


2. // pages/topic/topic.js

choseTopic:function(event){
  var topicItem = event.currentTarget.dataset['item'];
  var pages = getCurrentPages();
  var prevPage = pages[pages.length - 2];  //上一个页面
  prevPage.updateTopic(topicItem);
  wx.navigateBack()
}

示例二:赋值(收货地址选择) --prevPage.setData

1. <!--pages/chooseAddress/chooseAddress.wxml-->
<input placeholder="收货人姓名" bindinput="inputUser" value="{{address.name}}" />
<input placeholder="收货人电话" bindinput="inputPhone" value="{{address.phone}}" />
<textarea placeholder="详细地址" bindinput="inputDetail" value="{{address.detail}}" ></textarea>
<button bindtap="addAddress">添加</button>

<radio-group class="radio-group" bindchange="radioChange">
	<view class="item" wx:for="{{addressList}}" wx:key="index">
	  <radio class="radio" value="{{index}}">
		<text>{{item.name}} -{{item.phone}}-{{item.detail}}</text>
	  </radio>
	</view>
</radio-group>


2. // pages/chooseAddress/chooseAddress.js

data: {
  addressList: [],
  address: {
  name: "",
  phone: "",
  detail: ""
}
},
radioChange:function(e){
	var pages = getCurrentPages();
	var prevPage = pages[pages.length - 2];  //上一个页面
	
	var index = e.detail.value;
	var address = this.data.addressList[index];
	prevPage.setData({
	  address: address
	})
	wx.navigateBack();
  },
posted @ 2020-03-10 17:27  阿浪阿浪  阅读(1625)  评论(0编辑  收藏  举报