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

添加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>
相关推荐
Nueuis43 分钟前
微信小程序前端面经
前端·微信小程序·小程序
_r0bin_3 小时前
前端面试准备-7
开发语言·前端·javascript·fetch·跨域·class
IT瘾君3 小时前
JavaWeb:前端工程化-Vue
前端·javascript·vue.js
potender3 小时前
前端框架Vue
前端·vue.js·前端框架
站在风口的猪11084 小时前
《前端面试题:CSS预处理器(Sass、Less等)》
前端·css·html·less·css3·sass·html5
程序员的世界你不懂4 小时前
(9)-Fiddler抓包-Fiddler如何设置捕获Https会话
前端·https·fiddler
MoFe14 小时前
【.net core】天地图坐标转换为高德地图坐标(WGS84 坐标转 GCJ02 坐标)
java·前端·.netcore
去旅行、在路上5 小时前
chrome使用手机调试触屏web
前端·chrome
Aphasia3115 小时前
模式验证库——zod
前端·react.js
lexiangqicheng6 小时前
es6+和css3新增的特性有哪些
前端·es6·css3