【编程式导航】

我们希望在路由跳转之前执行某一些功能。。。

  1. <template>
  2. <div id="app">
  3. <h2>这是App.vue组件的标题</h2>
  4. <router-link to="/home"> 首页 </router-link>
  5. <br>
  6. <router-link to="/sample" > 样本 </router-link>
  7. <br>
  8. <button @click="avent"> 事件跳转首页 </button>
  9. <router-view></router-view>
  10. </div>
  11. </template>
  12.  
  13. <script>
  14.  
  15. export default {
  16. name: 'App',
  17. methods : {
  18. avent() {
  19. console.log('跳转到首页');
  20. this.$router.push("/home");
  21. }
  22. }
  23. }
  24. </script>
  25.  
  26. <style lang="stylus">
  27. #app
  28. font-family Avenir, Helvetica, Arial, sans-serif
  29. -webkit-font-smoothing antialiased
  30. -moz-osx-font-smoothing grayscale
  31. text-align center
  32. color #2c3e50
  33. margin-top 60px
  34. </style>

效果:

【懒加载路由】

打包之后JS文件会非常庞大,影响到页面的加载

如果我们能让不同的路由,对应的组件分割成不同的代码块

路由被访问的时候才能被加载组件,这样就高效了

路由懒加载的作用是由路由对应的组件打包成一个小的JS代码块

只有这个组件的路由被访问时才会加载JS代码块

index.js中配置:

  1. import Vue from 'vue';
  2. import VueRouter from "vue-router"; // 导入路由插件
  3.  
  4. //import home from "../components/home";
  5. //import sample from "../components/sample";
  6.  
  7. const home = () => import('../components/home'); // 头部声明式 懒加载
  8.  
  9. Vue.use(VueRouter); //注入路由插件
  10. const routes = [
  11. { path : '/home', component : home },
  12. { path : '/sample', component : () => import('../components/sample') } // 对象内直接声明 懒加载
  13. ]; // 定义路由
  14. const router = new VueRouter({ // 创建路由实例
  15. routes,
  16. mode : 'history'
  17. });
  18. export default router; // 导出路由实例

不知道啥问题。。。

【路由嵌套】

在首页组件中又设置了两个连接路由:

  1. <template>
  2. <div>
  3. <h2>这是首页组件</h2>
  4. <p>这是首页的内容</p>
  5.  
  6. <router-link to="/news"> 新闻 </router-link>
  7. <br>
  8. <router-link to="/msg"> 消息 </router-link>
  9. </div>
  10. </template>
  11.  
  12. <script>
  13. export default {
  14. name: "home"
  15. }
  16. </script>
  17.  
  18. <style scoped>
  19.  
  20. </style>

可以看到首页下面这两个组件:

然后点击新闻发现,上一级的首页组件不能保存:

所以我们需要嵌套路由,首先

首页组件页设置view标签

其次,在indexjs种设置子组件,并放入新闻和消息组件:

  1. import Vue from 'vue';
  2. import VueRouter from "vue-router"; // 导入路由插件
  3.  
  4. import home from "../components/home";
  5. import sample from "../components/sample";
  6. import news from "../components/news";
  7. import msg from "../components/msg";
  8.  
  9. Vue.use(VueRouter); //注入路由插件
  10. const routes = [
  11. {
  12. path : '/home',
  13. component : home,
  14. children : [ // 配置子路由
  15. { path : '/news', component : news },
  16. { path : '/msg', component : msg }
  17. ]
  18. },
  19.  
  20. { path : '/sample', component : sample },
  21. ]; // 定义路由
  22. const router = new VueRouter({ // 创建路由实例
  23. routes,
  24. mode : 'history'
  25. });
  26. export default router; // 导出路由实例

看起来问题像是解决了

我们地址也一应该保证这样的层级关系:

index.js路由地址:

  1. { path : 'news', component : news },
  2. { path : 'msg', component : msg }

【路由传参】

路由跳转可以参数传递,参数分为两种:Params & QueryParams

【Params类型】

配置路由格式:

  1. /new/:id

这样就表示,我们在使用新闻组件的时候需要在后面传id参数

  1. /news/24

如果不给予参数,则组件不会显示

加上参数访问:

【QueryParams】

配置路由方式不变,但是采用的是原生URL参数传递

我这里没用,没有那个效果。。。

控制台也没报错。。。

【Router-Link传参】

  1. <router-link
  2. :to="{
  3. path : '/home/news',
  4. query : {
  5. id : 2,
  6. name : '张三',
  7. age : 23,
  8. gender : true,
  9. hobby : [1,2,3,4,5]
  10. }
  11. }"
  12. > 新闻 </router-link>

url地址渲染:

  1. http://192.168.2.174:8080/home/news?id=2&name=%E5%BC%A0%E4%B8%89&age=23&gender=true&hobby=1&hobby=2&hobby=3&hobby=4&hobby=5

【JS代码传参】

  1. <template>
  2. <div id="app">
  3. <h2>这是App.vue组件的标题</h2>
  4. <router-link to="/home"> 首页 </router-link>
  5. <br>
  6. <router-link to="/sample" > 样本 </router-link>
  7. <br>
  8. <button @click="avent"> 事件跳转首页 </button>
  9. <br>
  10. <button @click="toNews"> 事件跳转home2 </button>
  11. <router-view></router-view>
  12.  
  13. </div>
  14. </template>
  15.  
  16. <script>
  17.  
  18. export default {
  19. name: 'App',
  20. methods : {
  21. avent() {
  22. console.log('跳转到首页');
  23. this.$router.push("/home");
  24. },
  25. toNews() {
  26. this.$router.push({
  27. path : "/home/news",
  28. query : {
  29. id : 33,
  30. name : "阿伟",
  31. age : 32,
  32. gender : true,
  33. others : [1,3,5,7,9]
  34. }
  35. });
  36. }
  37. }
  38. }
  39. </script>
  40.  
  41. <style lang="stylus">
  42. #app
  43. font-family Avenir, Helvetica, Arial, sans-serif
  44. -webkit-font-smoothing antialiased
  45. -moz-osx-font-smoothing grayscale
  46. text-align center
  47. color #2c3e50
  48. margin-top 60px
  49. </style>

点击传递:

  1. http://localhost:8080/home/news?id=33&name=%E9%98%BF%E4%BC%9F&age=32&gender=true&others=1&others=3&others=5&others=7&others=9

【参数获取】

在新闻组件中增加一个控制台打印查看:

  1. <template>
  2. <div>
  3. <h2>新闻组建</h2>
  4. <p>新闻内容</p>
  5. </div>
  6. </template>
  7.  
  8. <script>
  9. export default {
  10. name: "news",
  11. created() {
  12. console.log(this.$route);
  13. }
  14. }
  15. </script>
  16.  
  17. <style scoped>
  18.  
  19. </style>

访问测试:

这种方法必须要求:id赋值,然后才可以查询参数赋值

试试参数重复赋值时情况如何?

其实在这里就很明确了,params就是我们配置的格式:

改格式要求必须注入参数:RestFul风格

而query可以给也可以不给

现在打印这个参数看看:

  1. console.log(this.$route.params.id);

效果:

【路由监听】

每次请求访问都会被获取到

  1. <template>
  2. <div id="app">
  3. <h2>这是App.vue组件的标题</h2>
  4. <router-link to="/home"> 首页 </router-link>
  5. <br>
  6. <router-link to="/sample" > 样本 </router-link>
  7. <br>
  8. <button @click="avent"> 事件跳转首页 </button>
  9. <br>
  10. <button @click="toNews"> 事件跳转home2 </button>
  11. <router-view></router-view>
  12.  
  13. </div>
  14. </template>
  15.  
  16. <script>
  17.  
  18. export default {
  19. name: 'App',
  20. methods : {
  21. avent() {
  22. console.log('跳转到首页');
  23. this.$router.push("/home");
  24. },
  25. toNews() {
  26. this.$router.push({
  27. path : "/home/news",
  28. query : {
  29. id : 33,
  30. name : "阿伟",
  31. age : 32,
  32. gender : true,
  33. others : [1,3,5,7,9]
  34. }
  35. });
  36. }
  37. },
  38. watch : {
  39. '$route.path' : function (newVal, oldVal) {
  40. console.log(newVal);
  41. }
  42. }
  43. }
  44. </script>
  45.  
  46. <style lang="stylus">
  47. #app
  48. font-family Avenir, Helvetica, Arial, sans-serif
  49. -webkit-font-smoothing antialiased
  50. -moz-osx-font-smoothing grayscale
  51. text-align center
  52. color #2c3e50
  53. margin-top 60px
  54. </style>

请求在这里都会被捕获到:

但是使用地址栏输入的方式就不会捕获

也就是说,这些都是我们在组件中使用的跳转,是由router执行的,这种方式的捕获仅限于router设置的

【$route & $router的区别?】

$router是vue-router的实例,导航到不同的URL中使用$router.push方法

$route是当前router的跳转对象,可获取name,path等等信息

【Vue】12 VueRouter Part2 路由与传参的更多相关文章

  1. VueJs(11)---vue-router(命名路由,命名视图,重定向别名,路由组件传参)

    vue-router 上篇文章讲了第一篇vue-router相关文章,文章地址:VueJs(10)---vue-router(进阶1) 一.命名路由 有时候,通过一个名称来标识一个路由显得更方便一些, ...

  2. Vue配置路由和传参方式及路由守卫!

    安装路由 npm i vue-router -S 引入路由 import VueRouter form VueRouter 注入路由模块 Vue.use(VueRouter) 定义路由匹配规则 let ...

  3. Vue-admin工作整理(四):路由组件传参

    路由组件传参:如果在一个页面中,需要根据路由去获得参数,去对页面进行一些逻辑处理,首先可以通过this.$router来获取路由实例的参数,这样页面组件和路由就进行了耦合,为了进行分离,更大程度复用, ...

  4. Vue(小案例_vue+axios仿手机app)_公共组件(路由组件传参)

    一.前言                    1.公共轮播图的实现                    2.组件传参,公共组件的实现 二.主要内容 1.公共轮播图的实现 (1)分析:当渲染不同的轮 ...

  5. vue 路由动态传参 (多个)

    动态传参 传值页面  客户列表clientList.vue 路由 router.js 配置路由 接收参数的页面  客户详情CustomerDetails.vue 通过this.$router.para ...

  6. vue初始化、数据处理、组件传参、路由传参、全局定义CSS与JS、组件生命周期

    目录 项目初始化 组件数据局部化处理 子组件 父组件 路由逻辑跳转 案例 组件传参 父传子 子组件 父组件 子传父 子组件 父组件 组件的生命周期钩子 路由传参 第一种 配置:router/index ...

  7. vue-router路由动态传参query和params的区别

    1.query方式传参和接收参数 //路由 { path: '/detail', //这里不需要参入参数 name: "detail", component: detail//这个 ...

  8. vue 路由 URL传参

    源码如下: import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) //全局使用该组件 const ro ...

  9. vue-router query和params传参(接收参数)$router $route的区别

    今天做项目时踩到了vue-router传参的坑(query和params),所以决定总结一下二者的区别. 直接总结干货!!! 1.query方式传参和接收参数 传参: this.$router.pus ...

  10. vue router 如何使用params query传参,以及有什么区别

    写在前面: 传参是前端经常需要用的一个操作,很多场景都会需要用到上个页面的参数,本文将会详细介绍vue router 是如何进行传参的,以及一些小细节问题.有需要的朋友可以做一下参考,喜欢的可以点波赞 ...

随机推荐

  1. codemirror-editor-vue3 输入框信息太多 输入框宽度溢出隐藏

    我们把div注释看下之前溢出的效果 因为有form表单在里面任何标签上面设置都是不行 因为有校验要显示校验的信息overflow是不起作用的 要是单独的codemirror-editor-vue3 编 ...

  2. AGC043

    AGC043 A.Range Flip Find Route 简单DP B.123 Triangle 推性质. 利用模运算将减法变成加法(在绝对值0/1的情况下). Giant Graph 类似于博弈 ...

  3. LeetCode 146. LRU CacheLRU缓存机制 (C++/Java)

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  4. NET9 提供HybridCache解决分布式缓存中存在的远程链接&序列化带来的性能问题

    下面是一个标准的IDistributedCache用例: public class SomeService(IDistributedCache cache) { public async Task&l ...

  5. C#.NET 微信上传电子小票

    HttpWebRequest 时,不认图片的Content-Type.Content-Type 实际是有传的. 报错内容:{"code":"PARAM_ERROR&quo ...

  6. 使用 OpenTelemetry 构建可观测性 03 - 导出

    上一个博文中,我提到如何使用 OpenTelemery 的特定语言 API 来收集遥测数据,包含手动和自动的埋点技术,这很重要!但是,收集遥测数据只是解决方案的第一步. 你需要把遥测数据路由转发到其他 ...

  7. Java代码规范equals, for continue

    代码规范equals, for continue 代码规范1 if(v.getPartner().contains("文案")){ } //修改成: if("文案&quo ...

  8. json字符串忽略null,忽略字段,首字母大写等gson,jackson,fastJson实现demo,T data JSON.parseObject json转换

    json字符串忽略null,忽略字段,首字母大写等gson,jackson,fastJson实现demo package com.example.core.mydemo.json.vo; import ...

  9. SQLBI_精通DAX课程笔记_01_DAX介绍

    一:函数式语言 DAX是一个函数式语言,应用于Analysis Services , PowerPivot , 和Power Bi . 二:共同与不同 2.1  共同点 DAX与PowerPivot  ...

  10. 快速上手Python编程

    前言 .center { width: auto; display: table; margin-left: auto; margin-right: auto } 类型 原理 优点 缺点 编译型语言 ...