08.vue-router动态路由匹配
动态匹配路由的基本用法
思考:
<!-有如下3个路由链接.->
<router-link to="/user/1">User1</router-link>
<router-link to="/user/2">User2</router-link>
<router-link to="/user/3">User3</router-link>
//定义如下三个对应的路由规则,是否可行? ? ?
{ path: /user/1', component: User }
{ path: /user/2', component: User }
{ path: '/user/3',ncomponent: User }
应用场景:通过动态路由参数的模式进行路由匹配
var router = new VueRouter ({
routes: [
//动态路径参数以冒号开头
( path: '/user/:id', component: uUser }
]
})
const User = {
//路由组件中通过$route.params获取路由参数
template: '<div>user {{$route.params.id }}</div>'
}
路由组件传递参数
$route与对应路由形成高度耦合,不够灵活,所以可以使用props将组件和路由解耦
1.props的值为布尔类型
const router = new VueRouter ({
routes: [
//如果props被设置为true, route.params 将会被设置为组件属性
( path: '/user/:id', component: user, props: true]
]
})
const User = {
props: ['id'], //使用props接收路由参数
template: '<div>用户ID:{{id}}</div>' // 使用路由参数
}
2.props的值为对象类型
const router = new vueRouter ((
routes: [
//如果props是一个对象,它会被按原样设置为组件属性
{ path: '/user/ :id', component: User, props: { uname: 'lisi', age: 12 }}
]
})
const User = {
props: ['uname', 'age'],
template: '<div>用户信息: {{ uname + ---' + agel]</div>'
}
3.props的值为函数类型
const router = new VueRouter ((
routes:[
//如果props 是一个函数,则这个函数接收route 对象为自己的形参
{ path: /user/:id',
component: User,
props: route => ({ uname: 'zs', age: 20, id: route.params.id })}
]
})
const User= {
props: ['uname', 'age', 'id'],
template: '<div>用户信息: {{ uname + 1---' + age + ---' + id}}</div>'
}
08.vue-router动态路由匹配的更多相关文章
- 浅谈vue之动态路由匹配
在日常开发过程中,可能会遇到一些类似于新闻详情页的内容,需要把所有详情页映射到同一组件上,这是动态路由匹配的应用场景之一.在使用的过程中,也遇到过一些小坑,此篇做个简要的总结说明: 基本使用 { pa ...
- vue router动态路由
<div id="#app"> <router-link to="/user/header">路由1</router-link&g ...
- Vue 实现动态路由及登录&404页面跳转控制&页面刷新空白解决方案
Vue实现动态路由及登录&404页面跳转控制&页面刷新空白解决方案 by:授客 QQ:1033553122 开发环境 Win 10 Vue 2.9.6 node-v ...
- Vue系列之 => 路由匹配
路由基本使用,加动画切换 1 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Vue-Router动态路由匹配
//重点在于路由出口 <p> <!-- 使用 router-link 组件来导航. --> <!-- 通过传入 `to` 属性指定链接. --> <!-- & ...
- vue的动态路由(登录之后拿到动态路由通过addRouters()动态添加路由)
登录后我们拿到路由动态路由,后端传的数据可能为这个 { path: '/index', meta: { title: '首页', icon: 'icon-shouye', tab_index: , / ...
- vue --》动态路由的实现 (根据用户的权限来访问对应的模块)
为了设置用户的访问权限,所以在一个vue工程中,往往需要前端和后端开发人员共同配合设置动态路由, 进而实现根据用户的权限来访问对应的模块 1.首先在登录页面,前端跟据后端返回的路由信息进行配置,配置后 ...
- Vue router 一个路由对应多个视图
使用命名路由 https://jsfiddle.net/posva/6du90epg/ <script src="https://unpkg.com/vue/dist/vue.js&q ...
- vue-router 动态路由匹配
export default new Router({ routes: [ { path: '/', name: 'HelloWorld', component: HelloWorld, }, { / ...
随机推荐
- Website Scraping with Python 阅读笔记
第一章 工程涉及的基本工具:requests, beautiful soup, scrapy. 法规与技术约定:read the Terms & Conditions and the Priv ...
- python及Django的json序列化
JSON是一种文本序列化格式(它输出的是unicode文件,大多数时候会被编码为utf-8),人们很容易进行阅读和编写.python自带的dumps方法很有用,能很容易将字典dict类型数据转化为js ...
- netty中的channelPipeline在编程中的作用
在netty编程中我们绝大多数是要是用nio的,nio相比传统的io更加高效,而nio中核心概念离不开channel,buffer,selector三个重要的对象. 那么在netty中有一个chann ...
- [Docker] - 不同容器之间相互访问的实现方式(例如:Client 访问 DB)
部署了两个独立的容器: Container #1 - Web ClientContainer #2 - SQL Server 不同容器间如何互访? 无法从 Container #1 访问到 Conta ...
- 在js中使用for和forEach遍历数组
数组的遍历 for var arr = [1, 2, 3, 4]; for (var i = 0; i < arr.length; i++){ arr[i]; } forEach var arr ...
- Equalizing Two Strings CodeForces - 1256F (思维)
大意: 给定两个串$s,t$, 每次操作任选长度$len$, 分别翻转$s,t$中一个长$len$的子串, 可以进行任意次操作, 求判断能否使$s$和$t$相同. 字符出现次数不一样显然无解, 否则若 ...
- Intellij IDEA最全的热键表(default keymap)
Editing Ctrl + Space Basic code completion (the name of any class, method or variable) Ctrl + Shift ...
- [golang]按图片中心旋转后的新图左顶点和原图左顶点的偏移量计算
1 前言 略,作为记录使用 2 代码 /** * @Author: FB * @Description: * @File: RotateSample.go * @Version: 1.0.0 * @D ...
- vsftp 安装配置(被动模式)
vi /etc/vsftpd/vsftpd.conf vsftp配置末尾添加 pasv_enable=YES pasv_min_port=10000 pasv_max_port=10030 防火墙端口 ...
- 深入理解JVM(三) -- 对象的内存布局和访问定位
一 对象的内存布局: 在HotSpot虚拟机中,对象在内存中存储的布局可以分为3块区域:对象头(Header),实例数据(Instance Data)和对齐填充(Padding). HotSpot的对 ...