Vue Router

webpack目录结构

image-20201215103557011.png

runtime-compiler和runtime-only的区别

1.runtime-compiler:

  • 过程:template -> ast(抽象语法树) -> render -> vdom(虚拟dom) ->UI

2.runtime-only:

  • 过程:render -> vdom -> UI
  • 特点:性能更高,代码更少更轻便

一、Vue-cil

二、Vue-Router

history

history类似于一个栈结构

history.back等价于history.go(-1)

history.forward等价于history.go(1)

history.replaceState(),用参数中的路由属性替换当前的url

history.pushState(),push路由属性值history栈中

1.Vue-Router框架的搭建

  1. //1.配置路由相关信息
  2. import VueRouter from 'vue-router'
  3. import Vue from 'vue'
  4. //2.通过Vue.use(插件),安装插件
  5. Vue.use()
  6. //3.创建VueRouter对象
  7. const router = new VueRouter({
  8. //配置路由与组件的关系
  9. routes: [
  10. ]
  11. })
  12. //4.导出VueRouter对象
  13. export default router

2.VueRouter的使用

  1. //创建路由组件:Home.vue、About.vue
  2. //配置映射关系:组件和路径的关系
  3. import Vue from 'vue'
  4. import VueRouter from 'vue-router'
  5. import Home from '../components/Home'
  6. Vue.use(VueRouter)
  7. const routes = [
  8. {
  9. path: '/',
  10. name: 'Home',
  11. component: Home
  12. },
  13. {
  14. path: '/about',
  15. name: 'About',
  16. // route level code-splitting
  17. // this generates a separate chunk (about.[hash].js) for this route
  18. // which is lazy-loaded when the route is visited.
  19. component: () => import(/* webpackChunkName: "about" */ '../components/About')
  20. }
  21. ]
  22. const router = new VueRouter({
  23. routes
  24. })
  25. export default router
  26. //使用路由:<router-link>和<router-view>,view相当于一个占位,表示页面应该展示的位置
  27. <template>
  28. <div id="app">
  29. <h2>嘿嘿</h2>
  30. <router-link to="/">首页</router-link>
  31. <router-link to="/about">关于</router-link>
  32. <router-view></router-view>
  33. </div>
  34. </template>

3.路由使用的一些细节

  • redirect:重定向
  1. {
  2. path: '/',
  3. redirect: '/home'
  4. },
  5. {
  6. path: '/home',
  7. name: 'Home',
  8. component: Home
  9. },

image-20201215161518124.png

  • tag属性:默认为a标签,而可以使用tag属性来指定标签的样式
  • replace属性:不会留下history记录,该情况下不能通过后退按钮返回到上一个页面中
  • active-class:当路由匹配成功时,会自动给当前元素设置一个router-link-active的class

4.动态路由

作用:拿到url拼接的参数

  1. //App.vue,使用vbind绑定data中的数据到属性中
  2. <router-link :to="'/user/'+userId" tag="button">用户</router-link>
  3. <script>
  4. export default {
  5. name: 'App',
  6. data(){
  7. return {
  8. userId: 'zhangsan'
  9. }
  10. }
  11. }
  12. </script>
  13. //./router/index.js
  14. {
  15. path: '/user/:userId',
  16. name: 'User',
  17. component: () => import('../components/User')
  18. }
  19. //获取值
  20. <template>
  21. <div>
  22. <h2>这是用户组件啊</h2>
  23. <p>{{userId}}</p>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. name: "User",
  29. computed: {
  30. userId(){
  31. return this.$route.params.userId
  32. }
  33. }
  34. }
  35. </script>

5.懒加载

  • 路由懒加载的效果

image-20201216164813306.png

  • 路由懒加载的使用

component: () => import('../components/User')

5.路由嵌套

  1. //增加路由的子路由
  2. {
  3. path: '/home',
  4. name: 'Home',
  5. component: Home,
  6. children: [
  7. {
  8. path: 'news',
  9. component: () => import('../components/HomeNews')
  10. },
  11. {
  12. path: 'message',
  13. component: () => import('../components/HomeMessage')
  14. }
  15. ]
  16. }
  17. //2.在父路由中的模板添加router—view和router-link
  18. <template>
  19. <div>
  20. <h2>我是首页</h2>
  21. <p>我是首页内容</p>
  22. <router-link to="/home/news">新闻呀</router-link>
  23. <router-link to="/home/message">消息呀</router-link>
  24. <router-view></router-view>
  25. </div>
  26. </template>

6.路由间参数的传递

去参数的两种方式:

1.params类型:

  • 配置路由映射的path格式:’routerName/:id’
  • 将值放在路径后面
  • 在跳转后通过$route.params.id来获取参数

2.query类型:

  • 配置path格式:<router-link :to="{path: '/profile', query: {name: 'LewisLiu',age: 21}}" tag="button">档案</router-link>
  • 写好后,该参数值为作为路径后面的query参数进行传递
  • 通过$route.query来获取参数

7.导航守卫

使用组件的生命周期函数来实现自定义代码

  1. <script>
  2. export default {
  3. name: "Home",
  4. created() {
  5. document.title = '首页'
  6. },
  7. mounted() {
  8. },
  9. updated() {
  10. }
  11. }
  12. </script>

8.vue-router-keepalive

在路由切换时,使组件状态保留,避免重新渲染,有两个重要的属性

  • include-字符串或正则表达式,只有匹配的组件会被缓存
  • exclude-任何匹配的组件都不会被保存

三、Vuex

响应式状态管理

1.state

用于存储数据

2.mutations

跟新store状态的更新唯一方式:提交Mutation

  • 当更新一些数据时,可以携带参数,作为mutation的载荷
  • 如果参数不为一个时,可以将参数封装成一个对象进行参数传递
  1. // 1.组件中的method
  2. addStudent(){
  3. const stu = {id: 114, name: 'joker'}
  4. this.$store.commit('addStudent',stu)
  5. }
  6. // 2. /vuex/index.js中的方法
  7. addStudent(state,stu){
  8. state.student.push(stu)
  9. }

3.getters

同计算属性computed,可以将一些数据进行处理

4.actions

mutations中的方法要求为同步方法,对于异步方法,mutations不能很好的对devtools进行支持

故使用actions来进行异步操作

  1. actions: {
  2. aUpdateInfo(context,payload) {
  3. setTimeout(() =>{
  4. context.commit('updateInfo')//调用本身的mutations方法
  5. })
  6. }
  7. }

5.moudles

将store分模块