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.运行结果

首页

我的

商品

相关推荐
hongkid7 分钟前
React Native 如何打包正式apk
javascript·react native·react.js
李少兄9 分钟前
简单讲讲 SVG:前端开发中的矢量图形
前端·svg
前端小万11 分钟前
告别 CJS 库加载兼容坑
前端·前端工程化
恋猫de小郭11 分钟前
Flutter 3.38.1 之后,因为某些框架低级错误导致提交 Store 被拒
android·前端·flutter
JarvanMo15 分钟前
Flutter 需要 Hooks 吗?
前端
光影少年25 分钟前
前端如何虚拟列表优化?
前端·react native·react.js
Moment26 分钟前
一杯茶时间带你基于 Yjs 和 reactflow 构建协同流程图编辑器 😍😍😍
前端·后端·面试
菩提祖师_40 分钟前
量子机器学习在时间序列预测中的应用
开发语言·javascript·爬虫·flutter
invicinble44 分钟前
对于前端数据的生命周期的认识
前端
PieroPc1 小时前
用FastAPI 后端 和 HTML/CSS/JavaScript 前端写一个博客系统 例
前端·html·fastapi