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

添加vue-router

js 复制代码
npm install vue-router

配置路由

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

js 复制代码
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中注册路由,内容如下:

js 复制代码
import router from './router'
createApp(App).use(router).mount('#app')

配置路由链接

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

xml 复制代码
<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

<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>
相关推荐
程序员爱钓鱼28 分钟前
配置 GoLand 与 VS Code 开发环境
前端·后端·go
程序员爱钓鱼36 分钟前
Rust Vec 动态数组详解:创建、增删、遍历与排序
前端·后端·rust
LaughingZhu1 小时前
Product Hunt 每日热榜 | 2026-07-23
前端·神经网络·react.js·搜索引擎·前端框架
自然 醒1 小时前
记录We码开发者工具的一个bug
前端·javascript·bug
葡萄城技术团队1 小时前
HTML元素单元格:用自定义 CellType 扩展 SpreadJS 的显示能力
前端·javascript·html
不好听6131 小时前
Fragment:React 的 `<></>` 和原生的 DocumentFragment,同名不同命
前端·react.js·dom
不好听6131 小时前
useState 完全解析:初始化、异步更新、以及为什么传函数能解决闭包问题
前端·react.js
神明不懂浪漫3 小时前
【第七章】Java中的常用类
java·开发语言·前端·经验分享·笔记
GuWenyue9 小时前
放弃云端API!一套React+WebGPU本地LLM方案,零数据上传、离线可用
前端·人工智能·前端框架
原则猫10 小时前
函数/变量提升
前端