<template > <div class="product-wrap" > <div class="product" v-for="product in products" :key="product.id" @click="handleClick(product.id)"> <img :src="product.image" alt="" > <div> <span>{{ product.name }}</span> <h4>¥{{ product.price }}</h4> </div> <component :is="currentComponent"></component> </div> </div> </template> <script> export default { name: 'GoodsList', data() { return { products: [ { id: 'GoodsDetail01', name: '多普勒效应马克杯', price: 35.50, image: require('../assets/1_DopplerEffect_多普勒效应_White.png') }, { id: 'GoodsDetail02', name: '透镜成像公式马克杯', price: 40.99, image: require('../assets/3_LensEquation_透镜成像公式_White.png') }, { id: 3, name: '黑洞温度马克杯', price: 32.00, image: require('../assets/5_BlackHoleTemperature_黑洞温度_White.png') }, { id: 4, name: '爱因斯坦场方程马克杯', price: 45.00, image: require('../assets/6_EinsteinFieldEquations_爱因斯坦场方程_White.png') } ], currentComponent: null } }, methods:{ // handleClick(){ // this.$emit('click', 'GoodsDetail01') //点击后跳转到 GoodsDetail01 页面 // } handleClick(id) { this.currentComponent = id } } } </script> <style> .product-wrap { width: 1200px; margin: 50px auto; display: flex; justify-content: space-between } .product { border: 1px solid #ececee; border-radius: 5px; cursor: pointer; transition: .5s; } .product:hover { border: 1px solid #979797; box-shadow: 0px 0px 7px #979797; } .product img { width: 270px; padding: 10px; border-bottom: 1px solid #ececee; } .product>div { display: flex; justify-content: space-between; align-items: center; padding: 0 10px; } </style>goodslist商品主页点击跳转到goodsdetail01和goodsdetail02,不能用路由,使用动态组件跳转写出全部代码
时间: 2023-06-17 18:07:26 浏览: 72
以下是使用动态组件实现商品主页点击跳转到详情页的完整代码:
```
<template>
<div class="product-wrap">
<div class="product" v-for="product in products" :key="product.id" @click="handleClick(product.id)">
<img :src="product.image" alt="">
<div>
<span>{{ product.name }}</span>
<h4>¥{{ product.price }}</h4>
</div>
</div>
<component :is="currentComponent"></component>
</div>
</template>
<script>
import GoodsDetail01 from './GoodsDetail01.vue'
import GoodsDetail02 from './GoodsDetail02.vue'
export default {
name: 'GoodsList',
components: {
GoodsDetail01,
GoodsDetail02
},
data() {
return {
products: [
{
id: 'GoodsDetail01',
name: '多普勒效应马克杯',
price: 35.50,
image: require('../assets/1_DopplerEffect_多普勒效应_White.png')
},
{
id: 'GoodsDetail02',
name: '透镜成像公式马克杯',
price: 40.99,
image: require('../assets/3_LensEquation_透镜成像公式_White.png')
},
{
id: 'GoodsDetail03',
name: '黑洞温度马克杯',
price: 32.00,
image: require('../assets/5_BlackHoleTemperature_黑洞温度_White.png')
},
{
id: 'GoodsDetail04',
name: '爱因斯坦场方程马克杯',
price: 45.00,
image: require('../assets/6_EinsteinFieldEquations_爱因斯坦场方程_White.png')
}
],
currentComponent: null
}
},
methods: {
handleClick(id) {
this.currentComponent = id
}
}
}
</script>
<style>
.product-wrap {
width: 1200px;
margin: 50px auto;
display: flex;
justify-content: space-between
}
.product {
border: 1px solid #ececee;
border-radius: 5px;
cursor: pointer;
transition: .5s;
}
.product:hover {
border: 1px solid #979797;
box-shadow: 0px 0px 7px #979797;
}
.product img {
width: 270px;
padding: 10px;
border-bottom: 1px solid #ececee;
}
.product > div {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 10px;
}
</style>
```
在该代码中,我们使用了动态组件来实现商品详情页的跳转。首先,在`<template>`标签内,我们通过`v-for`指令遍历`products`数组,动态生成商品卡片,并在卡片上绑定了`handleClick()`方法,该方法会在用户点击商品卡片时被调用,将`currentComponent`属性设置为当前商品的`id`,从而实现动态组件的切换。
在`<script>`标签内,我们首先通过`import`语句引入了两个商品详情页的组件。然后,在`components`属性中注册了这两个组件,以便在后面使用。接着,我们定义了`data`属性,其中包含了商品数组`products`和当前组件的名称`currentComponent`。`handleClick()`方法用于在用户点击商品卡片时设置`currentComponent`属性。
最后,在`<style>`标签内,我们定义了一些样式,用于美化商品卡片的外观。
阅读全文