vue路由导航简单实现

1.代码

安装路由模块

npm install vue-router

导入路由组件:

import { createRouter, createWebHistory } from 'vue-router'

首先创建三个vue组件显示路由内容:

index.vue

复制代码
<template>
    <!-- 首页跳转 -->
     <router-link to="/my">我的</router-link>&nbsp;
     <router-link to="/goods">商品</router-link><br>
     <router-view></router-view>
     <h1>首页</h1>
</template>
<script setup>

</script>

<style scoped>

</style>

goods.vue

复制代码
<!--vue简单框架-->
<template>
<div class="">
    <router-link to="/">返回首页</router-link><br>
    <h1>商品订单</h1>
</div>
</template>

<script lang='ts' setup name="">
</script>

<style scoped>
</style>

my.vue

复制代码
<!--vue简单框架-->
<template>
<router-link to="/">返回首页</router-link><br>
<h1>个人中心</h1>
</template>

<script lang='ts' setup name="">

</script>

<style scoped>
</style>

App.vue:

复制代码
<template>
      <!-- 路由视图 -->
     <router-view></router-view>
</template>

<script setup>

</script>

<style scoped>

</style>

router文件下的index.js

复制代码
//导入组件
import { createRouter, createWebHistory } from 'vue-router'
import my from '../components/person/my.vue'
import goods from '../components/person/goods.vue'
import index from '../components/person/index.vue'
//配置路由
const routes=[
    {
        path:"/",
        component:index,
    },
    {
    path:"/my",
    component:my,
    },
    {
    path:"/goods",
    component:goods,
    }
];
//创建路由
const router=createRouter({
    routes,
   history:createWebHistory(),

})
//导出路由
export default router

main.js:

复制代码
import { createApp } from 'vue'
import App from './App.vue'
import router from '@/router'

createApp(App)
.use(router)
.mount('#app')

2.运行结果

首页

我的

商品

相关推荐
Hi~晴天大圣1 小时前
npm使用介绍
前端·npm·node.js
888CC++1 小时前
如何在 C 语言中进行程序调试?
前端·javascript·算法
喵个咪2 小时前
基于 Taro 的 Headless CMS 多端前端架构:技术解析与二次开发导引
前端·react.js·taro
狂炫冰美式2 小时前
你还在古法PPT吗,试试HTML呢?免费编辑导出工具给 xdm 放这了
前端·后端·github
万少3 小时前
未来组织的分水岭不是员工数量,而是人才密度
前端·后端·面试
任磊abc3 小时前
nextjs16配置eslint+prettier
前端·eslint·nextjs·prettier
x***r1513 小时前
Another-Redis-Desktop-Manager.1.3.7安装步骤详解(附Redis可视化连接与Key管理教程)
前端·bootstrap·html
Captaincc3 小时前
你真的知道自己把 AI 用在了哪里吗?这是 Vibe Usage 想回答的问题
前端·vibecoding
道友可好3 小时前
OpenSpec:轻到起飞的 AI 编程规范层
前端·人工智能·后端
kyriewen4 小时前
我招了一个“Prompt工程师”来写前端,结果项目差点崩了
前端·javascript·面试