老师 续上一个问题,上一个问题的回答也有回复,麻烦老师也帮忙看下
<template>
<div class="wrapper">
<div class="top">
<span class="iconfont top__icon"
@click="handleBackClick"></span>
<span class="top__title">编辑收货地址</span>
<span class="top__save"
@click="saveAddress">保存</span>
</div>
<form class="address">
<div class="address__same address__city">所在城市:<input type="text"
class="input"
placeholder="如厦门市"
v-model="info.city"></div>
<div class="address__same address__detail">详细地址:<input type="text"
class="input"
placeholder="如集美大学诚毅学院小高一"
v-model="info.detailAddress"></div>
<div class="address__same address__name">收货人:<input type="text"
class="input"
placeholder="请填写收货人的姓名"
v-model="info.name"></div>
<div class="address__same address__phone">联系电话:<input type="text"
class="input"
placeholder="请填写收货手机号"
v-model="info.phone"></div>
<div class="address__same address__default">是否设为默认地址: 是<input type="radio"
name="defaultElement"
v-model="info.defaultElement"
value="1"
class="address__default__yes">否<input type="radio"
name="defaultElement"
v-model="info.defaultElement"
value="0"
checked
class="address__default__no"></div>
</form>
<Toast v-if="showToast"
:message="toastMessage" />
</div>
</template>
<script>
import { useRouter, useRoute } from 'vue-router'
import { reactive, toRefs } from 'vue'
import { post } from '../../utils/request'
import Toast, { useToastEffect } from '../../components/Toast.vue'
// 根据ID获取当前地址信息
const getAddressEffect = () => {
const route = useRoute()
const userAddress = reactive({
info: []
})
const getAddress = async () => {
// const result = await post(`/address/getById?id=${route.params.id}`)
// if (result?.code === 1 && result?.data) {
// userAddress.info = result.data
// }
userAddress.info = JSON.parse(route.params.id)
console.log('getAddress info:' + JSON.stringify(info))
}
getAddress()
const { info } = toRefs(userAddress)
return { info }
}
// 处理编辑地址相关逻辑
const useNewAddressEffect = (changeToast) => {
const router = useRouter()
const route = useRoute()
const data = reactive({
city: '',
detailAddress: '',
name: '',
phone: '',
defaultElement: 0
})
const saveAddress = async () => {
try {
const { city, detailAddress, name, phone } = data
if (city === '' || detailAddress === '' || name === '' || phone === '') {
changeToast('地址、收货人、电话不能为空')
return false
} else {
const reg = /^1[3|4|5|7|8|9][0-9]\d{8}$/
if (reg.test(phone)) {
changeToast()
} else {
return changeToast('请输入正确的手机号')
}
}
console.log('route.params.id:' + route.params.id)
const result = await post('/address/update', {
id: route.params.id,
city: data.city,
detailAddress: data.detailAddress,
name: data.name,
phone: data.phone,
defaultElement: data.defaultElement
})
if (result?.code === 1) {
changeToast('地址更新成功成功')
setTimeout(() => {
router.push({ name: 'Address' })
}, 2000)
} else {
changeToast('修改失败')
}
} catch (e) {
changeToast('请求失败')
}
}
const { city, detailAddress, name, phone, defaultElement } = toRefs(data)
return { city, detailAddress, name, phone, defaultElement, saveAddress }
}
// 点击回退逻辑
const useBackRouterEffect = () => {
const router = useRouter()
const handleBackClick = () => {
router.back()
}
return handleBackClick
}
export default {
name: 'UpdateAddress',
components: { Toast },
setup () {
const handleBackClick = useBackRouterEffect()
const { showToast, toastMessage, changeToast } = useToastEffect()
const { info } = getAddressEffect()
const { city, detailAddress, name, phone, defaultElement, saveAddress } = useNewAddressEffect(changeToast)
return { info, handleBackClick, showToast, toastMessage, city, detailAddress, name, phone, defaultElement, saveAddress }
}
}
</script>
<style lang="scss" scoped>
@import "../../style/viriables.scss";
.wrapper {
overflow-y: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: #f8f8f8;
}
.top {
position: relative;
height: 0.44rem;
line-height: 0.44rem;
background: $bgColor;
margin-bottom: 0.12rem;
&__icon {
font-size: 0.22rem;
position: absolute;
left: 0.18rem;
}
&__title {
font-size: 0.16rem;
color: $content-fontcolor;
position: absolute;
left: 50%;
margin-left: -0.48rem;
}
&__save {
font-size: 0.14rem;
position: absolute;
right: 0.18rem;
}
}
.address {
box-sizing: border-box;
background: $bgColor;
padding: 0 0.18rem;
&__same {
height: 0.44rem;
line-height: 0.44rem;
border-bottom: 0.01rem solid $content-bgColor;
font-size: 0.14rem;
color: $medium-fontColor;
display: flex;
}
.input {
flex: 1;
border-style: none;
outline: medium;
}
&__default {
&__yes {
width: 0.5rem;
position: relative;
top: 0.16rem;
}
&__no {
width: 0.5rem;
position: relative;
top: 0.16rem;
}
}
}
</style>
正在回答
同学你好,对于你的问题解答如下:
1、因为template中双向绑定的数据info,而点击保存事件函数中,新声明了一个常量data,如下:
data中属性值都为空,与绑定的数据info也无任何关系,所以无法实现效果。
可以参考如下思路调整:
(1)将info作为参数传入useNewAddressEffect方法中,如下:
(2)useNewAddressEffect方法中接收传入的info数据,并针对这个数据进行处理,如下:
(3)测试结果如下:
2、单选按钮这里的实现可以参考如下思路实现:
(1)由于给“否”选项添加了checked属性,导致一直是该选项被选中,建议去掉,如下:
(2)Address.vue组件中模拟的数据结构中,每一项添加一个属性defaultElement,1表示为默认地址,0表示非默认地址。示例:
(3)测试结果如下:
点击第一项进入编辑页面
点击第二项进入编辑页面
祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星