路由----商品列表转详情

这是一个关于Vue.js的示例,展示了如何创建一个商品列表页面和详细信息页面。在列表页面中,商品的名称、价格和删除操作被显示,并通过Vue Router实现跳转到商品详情页。在详情页,商品的ID、名称和价格以RMB格式展示。应用使用axios进行API调用获取数据,并处理了跨域问题。
摘要由CSDN通过智能技术生成

列表`

<template>
  <div class="home">
    <!-- <img alt="Vue logo" src="../assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/> --> 
    <h2>
      商品列表
    </h2>
    <ul clas="item-list">
      <li class="head">
        <span>名称</span>
        <span>价格</span>
        <span>操作</span>

      </li>
      <li v-for="item of list" :key="item.id">
        <span>
          <router-link :to="{name:'items',params:{itemId:item.id}}">{{item.name}}</router-link>
        </span>
        <span>
          {{item.price}}
        </span>
        <span>
          <button>删除</button>
        </span>
      </li>
    </ul>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'HomeView',
  data(){
    return{
      list:[],
    }
  },
  created(){
    this.axios({
      url:'/api/items'
    }).then(({data})=>{
      this.list=data
       console.log(this.list); 
    });
  },
  components: {
    // HelloWorld
  },
}
</script>
<style>
ul{
  margin:0;
  padding:0;
}
li{
  list-style:none;
  width:510px;
  height:30px;
   display:flex;
  border-bottom:1px solid #333;
}

.item-list{
  padding:10px;
  /* display:flex; */
  justify-content:space-between;
  height:30px;
  line-height:30px;
  /* border-bottom:1px solid #333; */
}
.item-list li.head{
  font-weight:bold;
}
.item-list li span{
  min-width:200px;
}
span{
  width:200px;
  height:30px;
  line-height:30px;
}
</style>

详情

<template>
    <div>
        <div v-if="item">
            <h2>商品详情</h2>
            <dt>ID</dt>
            <dd>{{item.id}}</dd>
            <dt>名称</dt>
            <dd>{{item.name}}</dd>
            <dt>价格</dt>
            <dd>{{item.price | RMB}}</dd>
        </div>
        <div v-else>
            <h2>请稍后,暂时没有商品信息</h2>
        </div>
    </div>
</template>
<script>
import RMB from '@/filters/RMB.js'
export default{
    name:'items',
    data(){
        return{
            item:false
        }
    },
    filters:{
        RMB
    },
    mounted(){
        //获取路由参数(也就是得到要显示那组数据
        // console.log(typeof this.$route.params.itemId)
        let itemId=this.$route.params.itemId;
        if(itemId){
            this.axios({
                url:`/api/item/${itemId}`,
            }).then(res=>{
                console.log(res);
                this.item=res.data

            });
        }
    },
}
</script>
<style>

</style>

main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'

Vue.config.productionTip = false

Vue.prototype.axios=axios;

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

自定义个插件

export default val=>{
    return '¥'+(val/100).toFixed(2);
}

vue.config.js解决跨域
const { defineConfig } = require(‘@vue/cli-service’)
module.exports = defineConfig({
transpileDependencies: true,
devServer:{
proxy:{
‘/api’:{
target:“http://localhost:7777”,
pathRewrite:{
‘^/api’:‘’
}
}
}
}
})
路由

import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path:'/items/:itemId',
    name:'items',
    component:()=>import('@/views/items.vue') 
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

列表效果图
请添加图片描述
详情效果图

请添加图片描述**

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值