goodslist主页<template > <div class="product-wrap" @click="handleClick"> <div class="product" v-for="product in products" :key="product.id"> <img :src="product.image" alt="" > <div> <span>{{ product.name }}</span> <h4>¥{{ product.price }}</h4> </div> </div> </div> </template>,<template> <div class="container"> <div class="detail-wrap"> <img :src="imgSrc" alt=""> <div> <h1>{{ msg }}</h1> <p><span>¥</span><span>{{ price }}</span></p> <div><input type="number" v-model="num"></div> <button v-on:click="addToCart">加入购物车</button> </div> </div> </div> </template> goodsdetail01页,<template> <div class="container"> <div class="detail-wrap"> <img :src="imgSrc" alt=""> <div> <h1>{{ msg }}</h1> <p><span>¥</span><span>{{ price }}</span></p> <div><input type="number" v-model="num"></div> <button v-on:click="addToCart">加入购物车</button> </div> </div> </div> </template>GoodsDetail02页,实现主页点击列表图片跳转到其余两页,不能用路由实现
时间: 2023-06-18 21:06:46 浏览: 218
可以使用Vue的动态组件来实现。
在goodslist主页的模板中,给product-wrap添加一个@click事件,将点击事件绑定到一个方法handleClick上,方法中通过传递参数来决定渲染哪个组件。
```
<template>
<div class="product-wrap" @click="handleClick">
<div class="product" v-for="product in products" :key="product.id">
<img :src="product.image" alt="" >
<div>
<span>{{ product.name }}</span>
<h4>¥{{ product.price }}</h4>
</div>
</div>
</div>
</template>
<script>
import GoodsDetail01 from './GoodsDetail01.vue';
import GoodsDetail02 from './GoodsDetail02.vue';
export default {
data() {
return {
products: [
{ id: 1, name: '商品1', price: 100, image: 'img1.jpg' },
{ id: 2, name: '商品2', price: 200, image: 'img2.jpg' },
{ id: 3, name: '商品3', price: 300, image: 'img3.jpg' },
],
};
},
methods: {
handleClick(product) {
if (product.id === 1) { // 根据商品id来判断渲染哪个组件
this.$root.$emit('change-component', GoodsDetail01);
} else if (product.id === 2) {
this.$root.$emit('change-component', GoodsDetail02);
}
},
},
};
</script>
```
在主页的script标签中,定义两个组件GoodsDetail01和GoodsDetail02,并在methods中绑定change-component事件来动态切换组件。
```
<script>
import GoodsDetail01 from './GoodsDetail01.vue';
import GoodsDetail02 from './GoodsDetail02.vue';
export default {
components: {
GoodsDetail01,
GoodsDetail02,
},
data() {
return {
currentComponent: null,
};
},
created() {
this.$root.$on('change-component', (component) => {
this.currentComponent = component;
});
},
};
</script>
```
在主页的模板中,使用动态组件来渲染当前的组件。
```
<template>
<div>
<div class="product-wrap" @click="handleClick">
<div class="product" v-for="product in products" :key="product.id">
<img :src="product.image" alt="" >
<div>
<span>{{ product.name }}</span>
<h4>¥{{ product.price }}</h4>
</div>
</div>
</div>
<component :is="currentComponent"></component>
</div>
</template>
```
这样,就可以在点击主页的商品列表图片时,动态切换到对应的商品详情页,实现了跳转的效果。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="com.test.bean.Goods,java.util.ArrayList" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>购物车</title> <style type="text/css"> table{border-collapse:collapse;} td{border:1px solid black; text-align:center; } #deal{margin-left:200px} </style> </head> <body> <jsp:useBean id="cart" class="com.test.bean.Cart" scope="session"></jsp:useBean> <%if(cart==null||cart.getGoodslist().size()==0) out.println("购物车空空如也.....返回商品首页"); else{ ArrayList<Goods>goodslist=cart.getGoodslist(); %> 当前购物车共有<%=cart.getGcount() %>件物品 返回商品首页
序号 商品号 商品名称 价格 数量 小计 描述 <%for(int i=0;i<goodslist.size();i++){ Goods goods=goodslist.get(i); %> <%=i+1 %> <%=goods.getGid() %> <%=goods.getGname() %> <%= String.format("%.2f", goods.getGprice())%> <%=goods.getGcount() %> <%=String.format("%.2f", goods.getGprice()*goods.getGcount()) %> 删除 <%}%>
总计<%=String.format("%.2f",cart.getTotal())%> <input type="button" id="deal" value="购买" /></input> <%}%> <script type="text/javascript"> var dealbutton=document.getElementById("deal"); dealbutton.onclick=function(){ var cf=confirm("确定购买吗?"); if(cf==true){ window.location.href="Servlet/DealServlet"; } } </script> </body> </html> 修改删除功能,在购物车物品数量大于1的情况下点击“删除”时减少1个,等于1时删除此项商品;
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)