<template> <div id="app"> <!-- <img alt="Vue logo" src="./assets/logo.png"> <HelloWorld msg="Welcome to Your Vue.js App"/> --> <GoodsList @click="onClick"/> <GoodsDetail01 /> <GoodsDetail02/> </div> </template> <script> import GoodsList from './components/goodsList.vue'; import GoodsDetail01 from './components/goodsDetail01.vue'; import GoodsDetail02 from './components/goodsDetail02.vue'; // import HelloWorld from './components/HelloWorld.vue' export default { name: 'App', data(){ return{ active: 'GoodsList' }; }, methods:{ onClick(name){ this.name = name; } }, components: { // HelloWorld, GoodsList, GoodsDetail01, GoodsDetail02 } } </script>怎么改成展示主页Goodslist,点击主页列表图片实现跳转到其他页面
时间: 2023-06-19 14:04:54 浏览: 220
你可以在 `GoodsList` 组件中添加一个点击事件,然后在该事件中触发路由跳转,具体操作如下:
1. 在 `GoodsList` 组件中添加点击事件 `handleClick`,并传递当前商品的 id:
```html
<template>
<div class="goods-item" @click="handleClick(goods.id)">
<img :src="goods.imgUrl" alt="" />
<div class="goods-name">{{ goods.name }}</div>
<div class="goods-price">{{ goods.price }}</div>
</div>
</template>
<script>
export default {
props: {
goods: {
type: Object,
required: true
}
},
methods: {
handleClick(id) {
this.$router.push(`/goods/${id}`);
}
}
};
</script>
```
2. 在 `App` 组件中,将 `GoodsList` 组件作为默认展示的组件,并在 `onClick` 方法中更新 `active` 属性:
```html
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
import GoodsList from "./components/goodsList.vue";
export default {
name: "App",
components: {
GoodsList
},
data() {
return {
active: "GoodsList"
};
},
methods: {
onClick(name) {
this.active = name;
}
}
};
</script>
```
3. 在路由文件中,配置路由规则:
```js
import Vue from "vue";
import VueRouter from "vue-router";
import GoodsDetail01 from "./components/goodsDetail01.vue";
import GoodsDetail02 from "./components/goodsDetail02.vue";
import GoodsList from "./components/goodsList.vue";
Vue.use(VueRouter);
const routes = [
{
path: "/",
component: GoodsList
},
{
path: "/goods/:id",
component: GoodsDetail01
},
{
path: "/goods/:id/detail",
component: GoodsDetail02
}
];
const router = new VueRouter({
routes
});
export default router;
```
以上是一个简单的跳转实现,你可以根据需求进行进一步的开发和优化。
阅读全文