路由回退
使用 back 方法回到上一页
const router = useRouter()
const goBack = () => {
router.back()
}
路由传参
实现点击商店信息跳转到商店详情页
修改商店详情页的路由,添加id
{
path: '/shop/:id',
name: 'Shop',
component: () => import(/* webpackChunkName: "shop" */ '@/views/Shop.vue'),
},
router-link跳转时传入id
<router-link :to="`/shop/${info._id}`" v-for="info in nearbyList" :key="info._id">
<ShopInfo :info="info" />
</router-link>
使用 useRoute 获得 route,通过 route 获取 params
获取参数后,向端口 /api/shop/:id 发起请求,获得商店详情数据
const route = useRoute()
const shopInfo = ref({})
const getShopInfo = async () => {
const result = await get(`/api/shop/${route.params.id}`)
shopInfo.value = result.data.shopInfo
}
getShopInfo()
mock Restful数据
fastmock当接口为 /api/shop/:id 时, 通过 _req.params.id 获取路由参数
{
"code": "0000",
"data": {
"shopInfo": function({_req, Mock}) {
let id = _req.params.id;
if (id === "1") {
return {
"_id": "1",
"name": "沃尔玛",
"imgUrl": "https://img.ixintu.com/download/jpg/20201225/240f349847fc4942a955c02def66a158_512_512.jpg!bg",
"sales": "10000",
"expressLimit": "0",
"expressPrice": "5",
"slogan": "VIP尊享满89减4元运费券"
}
} else if (id === "2") {
return {
"_id": "2",
"name": "山姆会员店",
"imgUrl": "https://trgccw5gap-flywheel.netdna-ssl.com/wp-content/uploads/2011/11/Sams-Club-Logo.jpg",
"sales": "10000",
"expressLimit": "0",
"expressPrice": "5",
"slogan": "VIP尊享满89减4元运费券"
}
} else {
return null
}
}
},
"desc": "成功"
}
解决图片撕裂问题
因为图片还未加载就渲染了组件,导致图片显示撕裂状态,使用 v-if 或者 v-show 可解决
<shopInfo :info="shopInfo" v-show="shopInfo.imgUrl"/>