<vue 路由 2、router-link标签属性>
说明:在上一节的工程下继续讲解
一、 知识点说明
上一节中,
(1)replace: replace不会留下history记录, 所以指定replace的情况下, 后退键返回不能返回到上一个页面中
(2)active-class: 当
注:3.0以上版本删除了tag标签的用法
二、代码结构
注:主要是标红的几个文件
三、代码
重新编写这几个文件中的代码
index.js
//引入路由
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
//定义路由
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: () => import('../views/About.vue')
}
]
//创建路由
const router = createRouter({
//createWebHashHistory hash模式路径前面会多一个#号
history: createWebHistory(process.env.BASE_URL),
routes
})
//返回了路由
export default router
App.vue
Home.vue
Home Page
![]()
About.vue
This is an about page
四、效果
1、运行程序
注:要进入到相应的路劲下
启动成功后:
2、浏览器打开http://localhost:8080/
说明:Home是红色就是给class:router-link-active 添加的红色样式生效。点击About后
说明:About就变成红色,点击浏览器的返回上一页按钮
说明:这里并没有返回到Home的页面,而是返回到了一个空页面(空页面是浏览器打开时默认的一个页面),这就是
五、代码解释
无