api上已经有了once和emit的说明。直接上代码
触发监听
siteClick(e) {
let lastIndex = this.end.length;
console.log('end',lastIndex);
let index = e.currentTarget.dataset.index;
console.log(index);
console.log("执行siteClick()方法")
let key = 'site' + index
let that = this;
uni.$once(key, (data) => {
console.log('监听到事件来自 site ,携带参数 msg 为:' + data);
console.log('index',index);
if(index == 0){
that.gpsList[index].addressName = data.name
that.gpsList[index].addressDetail = data.addressName
that.gpsList[index].type = 'qd';
that.gpsList[index].contactName = data.contactName
that.gpsList[index].contactMobile = data.contactMobile
// 经度
that.gpsList[index].lnt = data.lnt
//维度
that.gpsList[index].lat = data.lat
}else if(index == lastIndex-1 || index == this.end-1){
that.gpsList[index].addressName = data.name
that.gpsList[index].addressDetail = data.addressName
that.gpsList[index].type = 'zd';
that.gpsList[index].contactName = data.contactName
that.gpsList[index].contactMobile = data.contactMobile
// 经度
that.gpsList[index].lnt = data.lnt
//维度
that.gpsList[index].lat = data.lat
}else{
that.gpsList[index].addressName = data.name
that.gpsList[index].addressDetail = data.addressName
that.gpsList[index].type = 'tjd';
that.gpsList[index].contactName = data.contactName
that.gpsList[index].contactMobile = data.contactMobile
// 经度
that.gpsList[index].lnt = data.lnt
//维度
that.gpsList[index].lat = data.lat
}
console.log('gpsList',this.gpsList)
})
uni.navigateTo({
url: '/pages/site/site?key=' + key,
})
},
site.vue页面
$emit传参给其他页面
<template>
<view class="page">
<view class="cu-bar search bg-white">
<view class="search-form round">
<text class="cuIcon-search"></text>
<input type="text" v-model="searchkey" @confirm="search" placeholder="搜索地点名称" confirm-type="search"></input>
</view>
<view class="action">
<button class="cu-btn bg-green shadow-blur round" @click="search">搜索</button>
</view>
</view>
<view class="cu-list menu card-menu margin sm-border" v-for="(item,index) in list" :key="index">
<view class="cu-item" @click="querySite(index)">
<view class="content padding-tb" style="width: 85%;">
<view class="flex">
<view class="flex-sub text-bold fl text-cut">{{item.name}}</view>
<view class="flex-sub text-center text-cut" v-if="item.contactName">
<text class="cuIcon-profile text-orange"></text>
{{item.contactName}}
<text class=".cuIcon-mobile text-orange"></text>
{{item.contactMobile}}
</view>
</view>
<view class="text-gray text-sm padding-top-xs ">
{{item.address}}</view>
</view>
</view>
</view>
</view>
</template>
<script>
import { getSiteList } from "@/api/site.js"
var _self;
export default {
data() {
return {
list:[],
searchkey:'',
key:''
}
},
onLoad(options) {
_self= this
var param={}
_self.key=options.key
console.log("传递的key="+options.key)
getSiteList(param).then((res)=>{
console.log(res)
_self.result =res.data.data.content
_self.list=res.data.data.content
})
},
methods: {
search(){
_self.list =_self.result.filter(item => item.name.includes(_self.searchkey))
},
querySite(i){
var site = _self.result[i]
console.log(site)
let waybillSite = {}
waybillSite.name=site.name
waybillSite.addressName=site.address
waybillSite.lnt= site.lnt
waybillSite.lat= site.lat
waybillSite.contactName=site.contactName
waybillSite.contactMobile=site.contactMobile
console.log(waybillSite)
uni.$emit(_self.key, waybillSite);
uni.navigateBack({});
}
}
}
</script>
<style>
</style>