目录
路由系统
- 多页项目的制作: 一个大型网站需要由多个页面组成
- 做法分两种:
- 传统方案: 每个网页是一个`html`文件, 通过超链接的href 来实现页面跳转
- 特点: 先清空之前的页面, 然后显示新的页面 -- 页面有闪烁的感觉
- 新方案: Ajax 局部切换
- 特点: 不需要清空整个页面, 只需要把局部内容删除 换成新的即可
- 新方案的专业名词: `SPA` - Single Page Application 单个页面应用
- 整个网站只有一个 html 页面, 利用局部内容切换来实现多页效果
- 这套方案依赖一款框架: Vue Router :https://router.vuejs.org/zh/
- 传统方案: 每个网页是一个`html`文件, 通过超链接的href 来实现页面跳转
- 路由占位符: 在实际显示时,会切换成对应的组件
- 自定义组件分两种:
- 组成页面的一部分, 目的是复用: 按钮;标签;超链接;存储在 components 中
- 组成页面的一部分, 目的是整体切换: 路由系统切换的页面存储在 views 中
路由流程
- 首先书写组件存放的占位符
- Vue会自动检测路径的值,通过路径前往router/index.js配置文件中寻找对应的组件
- 通过路径找到对应的组件,将组件替换掉占位符
路由配置
- 非懒加载: 先import 再使用
- import NewsPage from '../views/NewsPage.vue'
- path: 路径; 必须 / 开头
- component: 组件; 代表 路径对应的组件
- 懒加载 : 利用箭头函数引入, 临时调用函数时再引入
- 未使用前 不引入; 使用时 临时引入
懒加载和非懒加载的差异
- 路由配置文件中有两种配置方式
- 把`非常用`页面做成懒加载的 -- 首次加载时不加载, 使用时临时加载
- 把`常用`的页面做成非懒加载的 -- 首次加载时直接加载, 后续可以直接使用 提高体验
- 一种为非懒加载,先引入后配置
-
import Vue from "vue"; import VueRouter from "vue-router"; import HomeView from "../views/HomeView.vue"; Vue.use(VueRouter); const routes = [ { path: "/", name: "home", component: HomeView, //下面会讲到嵌套路由,这里先混个眼熟 children: [ { path: "/home/index", name: "routeName", meta: { keepAlive: true, }, component: () => import("../views/Home.vue"), }, { path: "/home/me", name: "routeName", component: () => import("../views/Me.vue"), }, ], }, ]
-
- 另一种为懒加载,直接配置
-
import Vue from "vue"; import VueRouter from "vue-router"; Vue.use(VueRouter); const routes = [ { path: "/login", name: "routeName", component: () => import("../views/Login.vue"), }, { path: "/BinPage", name: "routeName", component: () => import("../views/BinPage.vue"), }, { path: "/AboutView", name: "routeName", component: () => import("../views/AboutView.vue"), }, ]
-
- 懒加载与非懒加载的差异
嵌套路由
- 嵌套路由可以通过二级路由(多级路由)的方式动态修改页面的局部内容。这种页面中需要动态修改的内容由路由来控制。
- 实现步骤:
- 先准备好3个组件:`Container.vue Table.vue Form.vue`。
- 在`router/index.js`中配置`Component`组件的子路由。
- 在`el-main`里,添加占位符。`<router-view/>`。
-
import Vue from "vue"; import VueRouter from "vue-router"; import HomeView from "../views/HomeView.vue"; Vue.use(VueRouter); const routes = [ { path: "/", name: "home", component: HomeView, children: [ { path: "/home/index", name: "routeName", meta: { keepAlive: true, }, component: () => import("../views/Home.vue"), }, { path: "/home/me", name: "routeName", component: () => import("../views/Me.vue"), }, ], }, ]
编程式跳转
- 页面跳转方式分两种: 标签式 和 编程式
- 标签式
- <router-link to="/">首页</router-link>
- to: 是router-link的必备属性, 不写会爆红
-
自带激活样式
- 模糊: `router-link-active`
- 精确: `router-link-exact-active -
<!-- 占位符: 路由切换的核心 --> <router-view />
-
<router-link to="/about">关于</router-link>
- 编程式
- 通过方法配置跳转 this.$router.push('/about')
- <button @click="goto('/news')">新闻</button>
- <button @click="goto('/login')">登录</button>
- 路由对象在 router/index.js 中初始化 并 导出,在 main.js 中被 引入 和 填写到 vue 对象里
- console.log(this.$router) //从vue中获取 路由对象
- push: 用于在路径中推入新的路径
- 报错: 如果重复跳转,则会抛出错误
- 跳转前先判断: 当前路径 和 要跳转到的路径是否相同
- 如果相同, 就不跳转
- if (this.$router.currentRoute.path == path) return
- 当前路由信息读取: this.$router.currentRoute 太长了
- 作者提供了简化的属性 $route, 可以快速读取
- if (this.$route.path == path) return 相当于 $route(){return this.$router.currentRoute}
路由参数
- 路由传参 类似于 get的传参
- 格式: 路径?参数=值&参数=值...
-
<router-link to="/about?name=xxx&age=22&phone=13688778888">关于xxx</router-link> <router-link to="/about?name=xxx&age=18&phone=15788779999">关于xxx</router-link>
- 传递的参数保存在query属性里
- $route.query.name
- $route.query.age
每日一句
盛年不重来,一日难再晨。
"美好的青春年华过去了就不会再来,一天不可能有两个早晨,要珍惜时光啊!"