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

首页

我的

商品

相关推荐
diudiu_333 小时前
XSS跨站脚本攻击
前端·xss
终极前端开发协会3 小时前
【web前端 - 齐枭飞】乾坤【qiankun】应用,主项目与子项目交互详细代码,里面有详细代码,可直接粘贴过去 直接应用 ,
前端·前端框架·交互
闲蛋小超人笑嘻嘻4 小时前
localStorage用法详解
前端
Swift社区4 小时前
用 RN 的渲染模型,反推 Vue 列表的正确拆分方式
前端·javascript·vue.js
Mr_chiu4 小时前
微前端从入门到精通:Vue开发者的大型应用架构演进指南
前端·架构
光影少年4 小时前
前端开发桌面应用开发,Flutter 与 Electron如何选?
javascript·flutter·electron
Violet_YSWY4 小时前
Vue-Pinia defineStore 语法结构
前端·javascript·vue.js
全栈陈序员4 小时前
v-if 和 v-for 的优先级是什么?
前端·javascript·vue.js·学习·前端框架·ecmascript
xinyu_Jina4 小时前
Info Flow:大规模列表渲染中的UI虚拟化、数据懒-加载与前端性能工程
前端·ui
GISer_Jing4 小时前
JD AI全景:未来三年带动形成万亿规模的人工智能生态
前端·人工智能·aigc