<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,这是<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>这是goodslist商品主页,实现goodslist点击跳转到GoodsDetail01和GoodsDetail02,请用动态组件实现跳转,写出vue.app和goodslist代码
时间: 2023-06-17 14:07:37 浏览: 133
牛牛视频答题小程序 <template name="tmpl-15-container">
Vue.app代码如下:
```html
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
export default {
name: "App",
};
</script>
```
goodslist代码如下:
```html
<template>
<div>
<h2>商品列表</h2>
<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>
</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: 1,
name: "商品1",
price: 100,
image: "https://via.placeholder.com/150",
},
{
id: 2,
name: "商品2",
price: 200,
image: "https://via.placeholder.com/150",
},
],
currentComponent: "",
};
},
methods: {
handleClick(id) {
if (id === 1) {
this.currentComponent = "GoodsDetail01";
} else if (id === 2) {
this.currentComponent = "GoodsDetail02";
}
},
},
};
</script>
```
阅读全文