1.代码
安装路由模块
npm install vue-router
导入路由组件:
import { createRouter, createWebHistory } from 'vue-router'
首先创建三个vue组件显示路由内容:
index.vue
<template>
<!-- 首页跳转 -->
<router-link to="/my">我的</router-link>
<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.运行结果
首页
我的
商品