【Vue】12 VueRouter Part2 路由与传参
【编程式导航】
我们希望在路由跳转之前执行某一些功能。。。
- <template>
- <div id="app">
- <h2>这是App.vue组件的标题</h2>
- <router-link to="/home"> 首页 </router-link>
- <br>
- <router-link to="/sample" > 样本 </router-link>
- <br>
- <button @click="avent"> 事件跳转首页 </button>
- <router-view></router-view>
- </div>
- </template>
- <script>
- export default {
- name: 'App',
- methods : {
- avent() {
- console.log('跳转到首页');
- this.$router.push("/home");
- }
- }
- }
- </script>
- <style lang="stylus">
- #app
- font-family Avenir, Helvetica, Arial, sans-serif
- -webkit-font-smoothing antialiased
- -moz-osx-font-smoothing grayscale
- text-align center
- color #2c3e50
- margin-top 60px
- </style>
效果:
【懒加载路由】
打包之后JS文件会非常庞大,影响到页面的加载
如果我们能让不同的路由,对应的组件分割成不同的代码块
路由被访问的时候才能被加载组件,这样就高效了
路由懒加载的作用是由路由对应的组件打包成一个小的JS代码块
只有这个组件的路由被访问时才会加载JS代码块
index.js中配置:
- import Vue from 'vue';
- import VueRouter from "vue-router"; // 导入路由插件
- //import home from "../components/home";
- //import sample from "../components/sample";
- const home = () => import('../components/home'); // 头部声明式 懒加载
- Vue.use(VueRouter); //注入路由插件
- const routes = [
- { path : '/home', component : home },
- { path : '/sample', component : () => import('../components/sample') } // 对象内直接声明 懒加载
- ]; // 定义路由
- const router = new VueRouter({ // 创建路由实例
- routes,
- mode : 'history'
- });
- export default router; // 导出路由实例
不知道啥问题。。。
【路由嵌套】
在首页组件中又设置了两个连接路由:
- <template>
- <div>
- <h2>这是首页组件</h2>
- <p>这是首页的内容</p>
- <router-link to="/news"> 新闻 </router-link>
- <br>
- <router-link to="/msg"> 消息 </router-link>
- </div>
- </template>
- <script>
- export default {
- name: "home"
- }
- </script>
- <style scoped>
- </style>
可以看到首页下面这两个组件:
然后点击新闻发现,上一级的首页组件不能保存:
所以我们需要嵌套路由,首先
首页组件页设置view标签
其次,在indexjs种设置子组件,并放入新闻和消息组件:
- import Vue from 'vue';
- import VueRouter from "vue-router"; // 导入路由插件
- import home from "../components/home";
- import sample from "../components/sample";
- import news from "../components/news";
- import msg from "../components/msg";
- Vue.use(VueRouter); //注入路由插件
- const routes = [
- {
- path : '/home',
- component : home,
- children : [ // 配置子路由
- { path : '/news', component : news },
- { path : '/msg', component : msg }
- ]
- },
- { path : '/sample', component : sample },
- ]; // 定义路由
- const router = new VueRouter({ // 创建路由实例
- routes,
- mode : 'history'
- });
- export default router; // 导出路由实例
看起来问题像是解决了
我们地址也一应该保证这样的层级关系:
index.js路由地址:
- { path : 'news', component : news },
- { path : 'msg', component : msg }
【路由传参】
路由跳转可以参数传递,参数分为两种:Params & QueryParams
【Params类型】
配置路由格式:
- /new/:id
这样就表示,我们在使用新闻组件的时候需要在后面传id参数
- /news/24
如果不给予参数,则组件不会显示
加上参数访问:
【QueryParams】
配置路由方式不变,但是采用的是原生URL参数传递
我这里没用,没有那个效果。。。
控制台也没报错。。。
【Router-Link传参】
- <router-link
- :to="{
- path : '/home/news',
- query : {
- id : 2,
- name : '张三',
- age : 23,
- gender : true,
- hobby : [1,2,3,4,5]
- }
- }"
- > 新闻 </router-link>
url地址渲染:
- 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代码传参】
- <template>
- <div id="app">
- <h2>这是App.vue组件的标题</h2>
- <router-link to="/home"> 首页 </router-link>
- <br>
- <router-link to="/sample" > 样本 </router-link>
- <br>
- <button @click="avent"> 事件跳转首页 </button>
- <br>
- <button @click="toNews"> 事件跳转home2 </button>
- <router-view></router-view>
- </div>
- </template>
- <script>
- export default {
- name: 'App',
- methods : {
- avent() {
- console.log('跳转到首页');
- this.$router.push("/home");
- },
- toNews() {
- this.$router.push({
- path : "/home/news",
- query : {
- id : 33,
- name : "阿伟",
- age : 32,
- gender : true,
- others : [1,3,5,7,9]
- }
- });
- }
- }
- }
- </script>
- <style lang="stylus">
- #app
- font-family Avenir, Helvetica, Arial, sans-serif
- -webkit-font-smoothing antialiased
- -moz-osx-font-smoothing grayscale
- text-align center
- color #2c3e50
- margin-top 60px
- </style>
点击传递:
- 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
【参数获取】
在新闻组件中增加一个控制台打印查看:
- <template>
- <div>
- <h2>新闻组建</h2>
- <p>新闻内容</p>
- </div>
- </template>
- <script>
- export default {
- name: "news",
- created() {
- console.log(this.$route);
- }
- }
- </script>
- <style scoped>
- </style>
访问测试:
这种方法必须要求:id赋值,然后才可以查询参数赋值
试试参数重复赋值时情况如何?
其实在这里就很明确了,params就是我们配置的格式:
改格式要求必须注入参数:RestFul风格
而query可以给也可以不给
现在打印这个参数看看:
- console.log(this.$route.params.id);
效果:
【路由监听】
每次请求访问都会被获取到
- <template>
- <div id="app">
- <h2>这是App.vue组件的标题</h2>
- <router-link to="/home"> 首页 </router-link>
- <br>
- <router-link to="/sample" > 样本 </router-link>
- <br>
- <button @click="avent"> 事件跳转首页 </button>
- <br>
- <button @click="toNews"> 事件跳转home2 </button>
- <router-view></router-view>
- </div>
- </template>
- <script>
- export default {
- name: 'App',
- methods : {
- avent() {
- console.log('跳转到首页');
- this.$router.push("/home");
- },
- toNews() {
- this.$router.push({
- path : "/home/news",
- query : {
- id : 33,
- name : "阿伟",
- age : 32,
- gender : true,
- others : [1,3,5,7,9]
- }
- });
- }
- },
- watch : {
- '$route.path' : function (newVal, oldVal) {
- console.log(newVal);
- }
- }
- }
- </script>
- <style lang="stylus">
- #app
- font-family Avenir, Helvetica, Arial, sans-serif
- -webkit-font-smoothing antialiased
- -moz-osx-font-smoothing grayscale
- text-align center
- color #2c3e50
- margin-top 60px
- </style>
请求在这里都会被捕获到:
但是使用地址栏输入的方式就不会捕获
也就是说,这些都是我们在组件中使用的跳转,是由router执行的,这种方式的捕获仅限于router设置的
【$route & $router的区别?】
$router是vue-router的实例,导航到不同的URL中使用$router.push方法
$route是当前router的跳转对象,可获取name,path等等信息
【Vue】12 VueRouter Part2 路由与传参的更多相关文章
- VueJs(11)---vue-router(命名路由,命名视图,重定向别名,路由组件传参)
vue-router 上篇文章讲了第一篇vue-router相关文章,文章地址:VueJs(10)---vue-router(进阶1) 一.命名路由 有时候,通过一个名称来标识一个路由显得更方便一些, ...
- Vue配置路由和传参方式及路由守卫!
安装路由 npm i vue-router -S 引入路由 import VueRouter form VueRouter 注入路由模块 Vue.use(VueRouter) 定义路由匹配规则 let ...
- Vue-admin工作整理(四):路由组件传参
路由组件传参:如果在一个页面中,需要根据路由去获得参数,去对页面进行一些逻辑处理,首先可以通过this.$router来获取路由实例的参数,这样页面组件和路由就进行了耦合,为了进行分离,更大程度复用, ...
- Vue(小案例_vue+axios仿手机app)_公共组件(路由组件传参)
一.前言 1.公共轮播图的实现 2.组件传参,公共组件的实现 二.主要内容 1.公共轮播图的实现 (1)分析:当渲染不同的轮 ...
- vue 路由动态传参 (多个)
动态传参 传值页面 客户列表clientList.vue 路由 router.js 配置路由 接收参数的页面 客户详情CustomerDetails.vue 通过this.$router.para ...
- vue初始化、数据处理、组件传参、路由传参、全局定义CSS与JS、组件生命周期
目录 项目初始化 组件数据局部化处理 子组件 父组件 路由逻辑跳转 案例 组件传参 父传子 子组件 父组件 子传父 子组件 父组件 组件的生命周期钩子 路由传参 第一种 配置:router/index ...
- vue-router路由动态传参query和params的区别
1.query方式传参和接收参数 //路由 { path: '/detail', //这里不需要参入参数 name: "detail", component: detail//这个 ...
- vue 路由 URL传参
源码如下: import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) //全局使用该组件 const ro ...
- vue-router query和params传参(接收参数)$router $route的区别
今天做项目时踩到了vue-router传参的坑(query和params),所以决定总结一下二者的区别. 直接总结干货!!! 1.query方式传参和接收参数 传参: this.$router.pus ...
- vue router 如何使用params query传参,以及有什么区别
写在前面: 传参是前端经常需要用的一个操作,很多场景都会需要用到上个页面的参数,本文将会详细介绍vue router 是如何进行传参的,以及一些小细节问题.有需要的朋友可以做一下参考,喜欢的可以点波赞 ...
随机推荐
- codemirror-editor-vue3 输入框信息太多 输入框宽度溢出隐藏
我们把div注释看下之前溢出的效果 因为有form表单在里面任何标签上面设置都是不行 因为有校验要显示校验的信息overflow是不起作用的 要是单独的codemirror-editor-vue3 编 ...
- AGC043
AGC043 A.Range Flip Find Route 简单DP B.123 Triangle 推性质. 利用模运算将减法变成加法(在绝对值0/1的情况下). Giant Graph 类似于博弈 ...
- LeetCode 146. LRU CacheLRU缓存机制 (C++/Java)
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- NET9 提供HybridCache解决分布式缓存中存在的远程链接&序列化带来的性能问题
下面是一个标准的IDistributedCache用例: public class SomeService(IDistributedCache cache) { public async Task&l ...
- C#.NET 微信上传电子小票
HttpWebRequest 时,不认图片的Content-Type.Content-Type 实际是有传的. 报错内容:{"code":"PARAM_ERROR&quo ...
- 使用 OpenTelemetry 构建可观测性 03 - 导出
上一个博文中,我提到如何使用 OpenTelemery 的特定语言 API 来收集遥测数据,包含手动和自动的埋点技术,这很重要!但是,收集遥测数据只是解决方案的第一步. 你需要把遥测数据路由转发到其他 ...
- Java代码规范equals, for continue
代码规范equals, for continue 代码规范1 if(v.getPartner().contains("文案")){ } //修改成: if("文案&quo ...
- 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 ...
- SQLBI_精通DAX课程笔记_01_DAX介绍
一:函数式语言 DAX是一个函数式语言,应用于Analysis Services , PowerPivot , 和Power Bi . 二:共同与不同 2.1 共同点 DAX与PowerPivot ...
- 快速上手Python编程
前言 .center { width: auto; display: table; margin-left: auto; margin-right: auto } 类型 原理 优点 缺点 编译型语言 ...