安装
npm install vue-router@4 或 pnpm install vue-router@4
创建router文件,建议以app.vue同级
import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
{
path: '/',
name: 'Home',
component: () => import('@/views/home.vue')
},
{
path: '/template',
name: 'Template',
component: () => import('../views/template/index.vue')
}
]
const router = createRouter({
history: createWebHashHistory(),
routes
})
export default router
在main.ts中:
import App from './App.vue'
import router from './router'//引入
const app = createApp(App)
app.use(router) //注册路由
app.mount('#app')
在app.vue中
<template>
<div>
<router-view></router-view>
</div>
</template>
在页面中使用 如home.vue
<div class="home">
<div @click="Totemplate">跳转</div>
</div>
import { useRouter } from 'vue-router'
const router = useRouter()
const Totemplate = () => {
router.push({ path: '/template' })
console.log(11)
}