vue学习笔记 十三、路由介绍
|
系列导航 | ||
|---|---|---|
一、效果:

二、项目结构截图

三、代码
index.js
import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
//路由的配置属组
//paht:路由路劲 必须以/开头 必填
//component:对应的路由组件 必填
//name:路由的名字
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
//按需引入
//如果没有访问/about 就不会加载这个组件 节约性能
component: () => import( '../views/About.vue')
},
{
path: '/test',
name: 'Test',
//按需引入
//如果没有访问/about 就不会加载这个组件 节约性能
component: () => import( '../views/Test.vue')
}
]
//创建路由对象
const router = createRouter({
//createWebHashHistory hash模式路径前面会多一个#号
history: createWebHistory(process.env.BASE_URL),
routes
})
export default router
About.vue
<template>
<h1>This is an about page</h1>
</template>
Home.vue
<template> <h1>This is an Home page</h1> </template>
Test.vue
<template>
<h1>This is an test page</h1>
</template>
浙公网安备 33010602011771号