使用Vue3开发商品管理器(一)

使用Vue3开发商品管理器(一)

JohnYu

2025-06-05283阅读1分钟

添加vue-router

复制代码
体验AI代码助手
代码解读
复制代码
npm install vue-router

配置路由

新建一个router文件夹,在router文件夹下新建index.ts文件,内容如下:

javascript 复制代码
js
体验AI代码助手
代码解读
复制代码
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {path:'/',component:()=>import('../views/Home.vue')},
    {path:'/manager',component:()=>import('../views/GoodManager.vue')},
    {path:'/adder',component:()=>import('../views/GoodAdder.vue')},
    {path:'/about',component:()=>import('../views/About.vue')},
  ]
});
export default router

注册路由

在main.ts中注册路由,内容如下:

javascript 复制代码
javascript
体验AI代码助手
代码解读
复制代码
import router from './router'
createApp(App).use(router).mount('#app')

配置路由链接

在App.vue中配置路由链接,内容如下:

xml 复制代码
html
体验AI代码助手
代码解读
复制代码
<template>
  <router-link to="/">首页</router-link> |
  <router-link to="/manager">商品管理</router-link> |
  <router-link to="/adder">商品添加</router-link> |
  <router-link to="/about">关于</router-link> |
  <hr>
  <router-view></router-view>
</template>

首页(商品分类): ./views/Home.vue

xml 复制代码
vue
体验AI代码助手
代码解读
复制代码
<template>
    <h1>管理首页</h1>
    <!-- 进行商品分类信息的渲染 -->
     <table>
        <thead>
            <tr><th>ID</th><th>名称</th><th>图片</th><th>商品种类</th><th>价格合计</th></tr>
        </thead>
        <tbody>
            <tr v-for="cat in categories" :v-key="cat.id">
                <td>{{ cat.id }}</td>
                <td>{{ cat.name }}</td>
                <td>    
                    <img :src="BASE_URL+'images/'+cat.image" width="100px">
                </td>
                <td>0</td><td>¥0.0</td>
            </tr>
        </tbody>
     </table>
</template>
<script lang="ts" setup>
    import { onMounted,ref } from 'vue'
    //后端服务的访问地址
    const BASE_URL="http://localhost:3000/"
    // const BASE_URL="http://johnyu.cn:3000/categories"
    //定义一个响应式数据,用于存储商品分类信息
    const categories=ref([])
   //在页面加载完成后从服务器获取商品分类信息
   //此处使用了JS异步函数
    onMounted(async ()=>{
        const resp=await fetch(BASE_URL+"categories");
        const data=await resp.json();
        categories.value=data;
    })
</script>
相关推荐
Hexene...9 分钟前
【前端Vue】el-dialog关闭后黑色遮罩依然存在如何解决?
前端·javascript·vue.js·elementui·前端框架
Jay_See10 分钟前
JC链客云——项目过程中获得的知识、遇到的问题及解决
前端·javascript·vue.js
普通码农26 分钟前
Element Plus 数字输入框箭头隐藏方案
前端
草字40 分钟前
css flex布局,设置flex-wrap:wrap换行后,如何保证子节点被内容撑高后,每一行的子节点高度一致。
前端·javascript·css
Slice_cy1 小时前
深入剖析Vue框架:实现精简的computed
前端
局i1 小时前
ES6 类与继承:现代 JavaScript 面向对象编程
前端·javascript·es6
白菜上路1 小时前
C# Web API Mapster基本使用
前端·c#
叫我詹躲躲1 小时前
偷偷收藏!前端老鸟绝不外传的150个JS插件,让你效率翻3倍…
前端·vue.js
会豪1 小时前
如何让自己的前端项目更优雅
前端
uhakadotcom1 小时前
致新人:如何编写自己的第一个VSCode插件,以使用@vscode/vsce来做打包工具为例
前端·面试·github