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

首页

我的

商品

相关推荐
BillKu4 小时前
Vue3 + Element-Plus 抽屉关闭按钮居中
前端·javascript·vue.js
DevilSeagull4 小时前
JavaScript WebAPI 指南
java·开发语言·javascript·html·ecmascript·html5
面向星辰5 小时前
html中css的四种定位方式
前端·css·html
Async Cipher5 小时前
CSS 权重(优先级规则)
前端·css
大怪v5 小时前
前端佬:机器学习?我也会啊!😎😎😎手“摸”手教你做个”自动驾驶“~
前端·javascript·机器学习
Liquad Li6 小时前
Angular 面试题及详细答案
前端·angular·angular.js
用户21411832636026 小时前
首发!即梦 4.0 接口开发全攻略:AI 辅助零代码实现,开源 + Docker 部署,小白也能上手
前端
gnip8 小时前
链式调用和延迟执行
前端·javascript
SoaringHeart8 小时前
Flutter组件封装:页面点击事件拦截
前端·flutter
杨天天.8 小时前
小程序原生实现音频播放器,下一首上一首切换,拖动进度条等功能
前端·javascript·小程序·音视频