uni-app 地址增加及修改 页面对象传递
<script>
export default {
data() {
return {
user:{}
}
},
onShow() {
this.userInfo()
},
methods: {
async userInfo() {
let res = await this.$api.user()
if (res.code == 1) {
this.user = res.data.addresses
}
},
async edit(item){
console.log('item123:'+item)
uni.navigateTo({
url:'./addAdress/addAdress?editItem='+encodeURIComponent(JSON.stringify(item))
})
},
async dele(id){
let res = await this.$api.address_remove({
"id": id
})
if (res.code == 1) {
this.userInfo()
this.$u.toast('修改成功')
} else {
this.$u.toast('修改失败')
}
}
}
}
</script>
#另一个页面接受值
<script>
export default {
data() {
return {
user: {},
isEdit:false,
show: false
}
},
onLoad(option) {
let editItem = JSON.parse(decodeURIComponent(option.editItem));
let user = {
"id": editItem['id'],
"name": editItem['name'],
"mobile": editItem['mobile'],
"detail": editItem['detail'],
"area": editItem['area']
}
this.user = user
this.isEdit = true
},
methods: {
changeAddress(e) {
console.log('label:'+e.province.label + e.city.label + e.area.label)
this.user.area = e.province.label + e.city.label + e.area.label
},
async addRes() {
if (!this.isEdit) {
let res = await this.$api.address_add({
"name": this.user.name,
"mobile": this.user.mobile,
"detail": this.user.detail,
"area": this.user.area
})
if (res.code == 1) {
this.$u.toast('新增成功')
uni.navigateBack({
delta:1
})
} else {
this.$u.toast('新增失败')
}
} else {
let res = await this.$api.address_update({
"id": this.user.id,
"name": this.user.name,
"mobile": this.user.mobile,
"detail": this.user.detail,
"area": this.user.area
})
if (res.code == 1) {
this.$u.toast('修改成功')
uni.navigateBack({
delta:1
})
} else {
this.$u.toast('修改失败')
}
}
}
}
}
</script>