1、vue-router 两种模式

(1)mode:hash,hash模式背后的原理是onhashchange事件,可以在window对象上监听这个事件。vue默认为hash模式

  1. window.onhashchange = function(event){
  2.  
  3. console.log(event.oldURL, event.newURL);
  4. let hash = location.hash.slice(1);
  5. document.body.style.color = hash;

(2)mode:history

  1. const router = new VueRouter({
  2. mode:"history",
  3. routes:[]
  4. })

不怕前进,不怕后退,就怕刷新F5,如果后端没有准备的话,刷新是实实在在地去请求服务器的。

在hash模式下,前端路由修改的是#中的信息,而浏览器请求时是不带它玩的,所以没有问题,但是在history下,你可以自由的修改path,当刷新时,如果服务器中没有相应的响应或者资源,会刷出一个404来。

2、嵌套路由

  1. <script src="https://unpkg.com/vue/dist/vue.js"></script>
  2. <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
  3.  
  4. <div id="app">
  5. <p>
  6. <router-link to="/user/foo">/user/foo</router-link>
  7. <router-link to="/user/foo/profile">/user/foo/profile</router-link>
  8. <router-link to="/user/foo/posts">/user/foo/posts</router-link>
  9. </p>
  10. <router-view></router-view>
  11. </div>
  1. const User = {
  2. template: `
  3. <div class="user">
  4. <h2>User {{ $route.params.id }}</h2>
  5. <router-view></router-view>
  6. </div>
  7. `
  8. }
  9.  
  10. const UserHome = { template: '<div>Home</div>' }
  11. const UserProfile = { template: '<div>Profile</div>' }
  12. const UserPosts = { template: '<div>Posts</div>' }
  13.  
  14. const router = new VueRouter({
  15. routes: [
  16. { path: '/user/:id', component: User,
  17. children: [
  18. // UserHome will be rendered inside User's <router-view>
  19. // when /user/:id is matched
  20. { path: '', component: UserHome },
  21.  
  22. // UserProfile will be rendered inside User's <router-view>
  23. // when /user/:id/profile is matched
  24. { path: 'profile', component: UserProfile },
  25.  
  26. // UserPosts will be rendered inside User's <router-view>
  27. // when /user/:id/posts is matched
  28. { path: 'posts', component: UserPosts }
  29. ]
  30. }
  31. ]
  32. })
  33.  
  34. const app = new Vue({ router }).$mount('#app')

3、嵌套命名视图

  1. <script src="https://unpkg.com/vue/dist/vue.js"></script>
  2. <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
  3.  
  4. <div id="app">
  5. <h1>Nested Named Views</h1>
  6. <router-view></router-view>
  7. </div>
  1. const UserSettingsNav = {
  2. template: `
  3. <div class="us__nav">
  4. <router-link to="/settings/emails">emails</router-link>
  5. <br>
  6. <router-link to="/settings/profile">profile</router-link>
  7. </div>
  8. `
  9. }
  10. const UserSettings = {
  11. template: `
  12. <div class="us">
  13. <h2>User Settings</h2>
  14. <UserSettingsNav/>
  15. <router-view class ="us__content"/>
  16. <router-view name="helper" class="us__content us__content--helper"/>
  17. </div>
  18. `,
  19. components: { UserSettingsNav }
  20. }
  21.  
  22. const UserEmailsSubscriptions = {
  23. template: `
  24. <div>
  25. <h3>Email Subscriptions</h3>
  26. </div>
  27. `
  28. }
  29.  
  30. const UserProfile = {
  31. template: `
  32. <div>
  33. <h3>Edit your profile</h3>
  34. </div>
  35. `
  36. }
  37.  
  38. const UserProfilePreview = {
  39. template: `
  40. <div>
  41. <h3>Preview of your profile</h3>
  42. </div>
  43. `
  44. }
  45.  
  46. const router = new VueRouter({
  47. mode: 'history',
  48. routes: [
  49. { path: '/settings',
  50. // You could also have named views at tho top
  51. component: UserSettings,
  52. children: [{
  53. path: 'emails',
  54. component: UserEmailsSubscriptions
  55. }, {
  56. path: 'profile',
  57. components: {
  58. default: UserProfile,
  59. helper: UserProfilePreview
  60. }
  61. }]
  62. }
  63. ]
  64. })
  65.  
  66. router.push('/settings/emails')
  67.  
  68. new Vue({
  69. router,
  70. el: '#app'
  71. })

vue-router 注意事项的更多相关文章

  1. python 全栈开发,Day91(Vue实例的生命周期,组件间通信之中央事件总线bus,Vue Router,vue-cli 工具)

    昨日内容回顾 0. 组件注意事项!!! data属性必须是一个函数! 1. 注册全局组件 Vue.component('组件名',{ template: `` }) var app = new Vue ...

  2. Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转

    问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...

  3. Vue 2.0 + Vue Router + Vuex

    用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...

  4. vue router 只需要这么几步

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. Vue.js 2.x笔记:路由Vue Router(6)

    1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...

  6. Vue Router学习笔记

    前端的路由:一个地址对应一个组件 Vue Router中文文档 一.路由基本使用 第1步:导入Vue Router: <script src="https://unpkg.com/vu ...

  7. vue router.push(),router.replace(),router.go()和router.replace后需要返回两次的问题

    转载:https://www.cnblogs.com/lwwen/p/7245083.html https://blog.csdn.net/qq_15385627/article/details/83 ...

  8. 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI

    一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...

  9. 深入浅出的webpack4构建工具---webpack+vue+router 按需加载页面(十五)

    1. 为什么需要按需加载? 对于vue单页应用来讲,我们常见的做法把页面上所有的代码都打包到一个bundle.js文件内,但是随着项目越来越大,文件越来越多的情况下,那么bundle.js文件也会越来 ...

  10. 深入浅出的webpack构建工具--webpack4+vue+router项目架构(十四)

    阅读目录 一:vue-router是什么? 二:vue-router的实现原理 三:vue-router使用及代码配置 四:理解vue设置路由导航的两种方法. 五:理解动态路由和命名视图 六:理解嵌套 ...

随机推荐

  1. mybatis学习系列五--插件及类型处理器

    2 插件编写(80-81) 单个插件编写 2.1实现interceptor接口(ibatis) invocation.proceed()方法执行必须要有,否则不会无法实现拦截作用 2.2 使用@int ...

  2. oracle的order by排序中空字符串处理方法

    1.缺省处理 Oracle在Order by 时缺省认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前 2.使用nvl函数 nvl函数可以将输入参数为空时转换为一特定值,如 ...

  3. python第五十二课--自定义异常类

    myexception.py ''' 实现自定义异常类: ''' class MyException(Exception): def __init__(self,msg): super().__ini ...

  4. Perfect Pth Powers pku-1730(筛+合数分解)

    题意:x可以表示为bp, 求这个p的最大值,比如 25=52, 64=26,  然后输入x 输出 p 就是一个质因子分解.算法.(表示数据上卡了2个小时.) 合数质因子分解模板. ]; ]; ; ;n ...

  5. P3265 [JLOI2015]装备购买(高斯消元+贪心,线性代数)

    题意; 有n个装备,每个装备有m个属性,每件装备的价值为cost. 小哥,为了省钱,如果第j个装备的属性可以由其他准备组合而来.比如 每个装备属性表示为, b1, b2.......bm . 它可以由 ...

  6. php 怎么在foreach中循环数组的时候添加元素的属性

    foreach($arr as $key => &$vo){ //注意,由于上面遍历的时候写了地址传值符&, //所以下面可以直接给$vo 赋值;如果不写&符号,下面这样 ...

  7. oracle创建em

    语法:emca -config dbcontrol db [-repos (create | recreate)] [-cluster] [-silent] [-backup] [parameters ...

  8. mysql函数之截取字符串

    文章摘取自http://www.cnblogs.com/zdz8207/p/3765073.html 练习截取字符串函数(五个) mysql索引从1开始 一.mysql截取字符串函数 1.left(s ...

  9. 【转】图像的上采样(upsampling)与下采样(subsampled)

    转自:https://blog.csdn.net/stf1065716904/article/details/78450997 参考: http://blog.csdn.net/majinlei121 ...

  10. win7/10下Qt Creator调试提示:The selected debugger may be inappropriate for the inferior的解决办法

    在win7/10下Qt Creator调试提示:The selected debugger may be inappropriate for the inferior的错误提示内容如下图所示: 一般弹 ...