Vue Router基础
路由
安装 vue-router
起步
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link> <router-view></router-view>
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' } const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
] const router = new VueRouter({
routes // (缩写) 相当于 routes: routes
}) const app = new Vue({
router
}).$mount('#app')
通过this.$route访问路由器
export default {
computed: {
username () {
return this.$route.params.username
}
},
methods: {
goBack () {
window.history.length >
? this.$router.go(-)
: this.$router.push('/')
}
}
}
动态路由
{ path: '/user/:id', component: User }
像/user/bar和/user/foo都能映射到相同的路由
参数会被设置到this.$route.params中
const User = {
template: '<div>User {{ $route.params.id }}</div>'
}
可以设置多端参数
// 模式
/user/:username/post/:post_id
// 匹配路径
/user/evan/post/
// $router.params
{ username: 'evan', post_id: '' }
从/user/foo到/user/bar 原来的组件实例会被复用。因为两个路由都渲染同一个组件。也就是组件不会销毁和创建,而是复用,也就是组件的生命周期钩子不会再调用
可以使用watch检测$router对象
const User = {
template: '...',
watch: {
'$route' (to, from) {
// 对路由变化作出响应...
}
}
}
捕获所有路由或404 Not found路由
匹配所有路由
path: '*'
匹配'/user-*'开头的路由
path: '/user-*'
当使用通配符时,$router.params会添加名为pathMatch参数
// 给出一个路由 { path: '/user-*' }
this.$router.push('/user-admin')
this.$route.params.pathMatch // 'admin'
// 给出一个路由 { path: '*' }
this.$router.push('/non-existing')
this.$route.params.pathMatch // '/non-existing'
嵌套路由
我们在User组件中添加router-view
const User = {
template: `
<div class="user">
<h2>User {{ $route.params.id }}</h2>
<router-view></router-view>
</div>
`
}
路由配置
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User,
children: [
{
// 当 /user/:id/profile 匹配成功,
// UserProfile 会被渲染在 User 的 <router-view> 中
path: 'profile',
component: UserProfile
},
{
// 当 /user/:id/posts 匹配成功
// UserPosts 会被渲染在 User 的 <router-view> 中
path: 'posts',
component: UserPosts
}
]
}
]
})
编程式导航
声明式 <router-link :to='' />
编程式 router.push()
const userId = ''
router.push({ name: 'user', params: { userId }}) // -> /user/123
router.push({ path: `/user/${userId}` }) // -> /user/123
// 这里的 params 不生效
router.push({ path: '/user', params: { userId }}) // -> /user
router.go(n)
// 在浏览器记录中前进一步,等同于 history.forward()
router.go() // 后退一步记录,等同于 history.back()
router.go(-) // 前进 3 步记录
router.go() // 如果 history 记录不够用,那就默默地失败呗
router.go(-)
router.go()
const router = new VueRouter({
routes: [
{
path: '/user/:userId',
name: 'user',
component: User
}
]
})
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
router.push({ name: 'user', params: { userId: }})
命名视图
一个组件渲染多个视图
<router-view class="view one"></router-view>
<router-view class="view two" name="a"></router-view>
<router-view class="view three" name="b"></router-view>
const router = new VueRouter({
routes: [
{
path: '/',
components: {
default: Foo,
a: Bar,
b: Baz
}
}
]
})
重定向
/a重定向到/b
const router = new VueRouter({
routes: [
{ path: '/a', redirect: '/b' }
]
})
也可以是命名路由
{ path: '/a', redirect: { name: 'foo' }}
也可以是方法
{ path: '/a', redirect: to => {
// 方法接收 目标路由 作为参数
// return 重定向的 字符串路径/路径对象
}}
别名
{ path: '/a', component: A, alias: '/b' }
路由组件传参
之前我们通过$router获得参数
const User = {
template: '<div>User {{ $route.params.id }}</div>'
}
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User }
]
})
通过props解耦
如果props设置为true,那么route.params将会被设置为组件属性
const User = {
props: ['id'],
template: '<div>User {{ id }}</div>'
}
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User, props: true }, // 对于包含命名视图的路由,你必须分别为每个命名视图添加 `props` 选项:
{
path: '/user/:id',
components: { default: User, sidebar: Sidebar },
props: { default: true, sidebar: false }
}
]
})
Vue Router基础的更多相关文章
- [Vue 牛刀小试]:第十三章 - Vue Router 基础使用再探(命名路由、命名视图、路由传参)
一.前言 在上一章的学习中,我们简单介绍了前端路由的概念,以及如何在 Vue 中通过使用 Vue Router 来实现我们的前端路由.但是在实际使用中,我们经常会遇到路由传参.或者一个页面是由多个组件 ...
- vue Router——基础篇
vue--Router简介 vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用. vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路 ...
- Vue技术点整理-Vue Router
路由 Vue Router 对于单页面应用来说,如果涉及到多个页面的话,就必须要使用到路由,一般使用官方支持的 vue-router 库 一,Vue Router 在项目中的安装引用 1,在页面中使用 ...
- vue Router——进阶篇
vue Router--基础篇 1.导航守卫 正如其名,vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的 ...
- react router @4 和 vue路由 详解(一)vue路由基础和使用
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 1.vue路由基础和使用 a.大概目录 我这里建了一个router文件夹,文件夹下有in ...
- 「vue基础」一篇浅显易懂的 Vue 路由使用指南( Vue Router 上)
大家好,今天的内容,我将和大家一起聊聊 Vue 路由相关的知识,如果你以前做过服务端相关的开发,那你一定会对程序的URL结构有所了解,我没记错的话也是路由映射的概念,需要进行配置. 其实前端这些框架的 ...
- 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI
一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...
- 深入浅出的webpack构建工具--webpack4+vue+router项目架构(十四)
阅读目录 一:vue-router是什么? 二:vue-router的实现原理 三:vue-router使用及代码配置 四:理解vue设置路由导航的两种方法. 五:理解动态路由和命名视图 六:理解嵌套 ...
- python 全栈开发,Day91(Vue实例的生命周期,组件间通信之中央事件总线bus,Vue Router,vue-cli 工具)
昨日内容回顾 0. 组件注意事项!!! data属性必须是一个函数! 1. 注册全局组件 Vue.component('组件名',{ template: `` }) var app = new Vue ...
随机推荐
- EXCEL数据计算不准确的问题
今天,某部门的excel的数值计算,总是出现错误.如下图 ,我们的46那一栏是有前面8*6得出来的,但是结果却显示46,明明应该是48才对,然后再往上追,8是有前面的337-329得出来的,337是有 ...
- 解决vcenter 6.0 vcsa安装插件第二个的时候报错的问题
解决vcenter 6.0 vcsa安装插件第二个的时候报错的问题 需要打一下windows 的Microsoft v C++ 2013的2个补丁就可以正常运行了. 然后在后续安装过程中,到达最后一步 ...
- day07 python列表 集合 深浅拷贝
day07 python 一.知识点补充 1."".join() s = "".join(('1','2','3','4','5')) #将字符串 ...
- SqlServer 查询的时候过滤条件有参数导致速度很慢的问题-参数嗅探
何谓SQLSERVER参数嗅探 大家听到“嗅探”这个词应该会觉得跟黑客肯定有关系吧,使用工具嗅探一下参数,然后截获,脱裤o(∩_∩)o . 事实上,我觉得大家太敏感了,其实这篇文章跟数据库安全没有什么 ...
- 简单API接口签名验证
前言 后端在写对外的API接口时,一般会对参数进行签名来保证接口的安全性,在设计签名算法的时候,主要考虑的是这几个问题: 1. 请求的来源是否合法 2. 请求参数是否被篡改 3. 请求的唯一性 我们的 ...
- pandas中读取文件报错
import pandas as pd fileName = "路径中带有中文/xxx.csv" tf_train = pd.read_csv(fileName) 会提示报错 OS ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- 『Golang』—— 标准库之 time
... package main import ( "fmt" "time" ) func main() { time.AfterFunc(time.Milli ...
- 【Java多线程系列五】列表类
一些列表类及其特性 类 线程安全 Iterator 特性 说明 Vector 是 fail-fast 内部方法用synchronized修饰,因此执行效率较低 1. 线程安全的列表类并不意味着调用它 ...
- cesium安装及第一个示例
cesium安装及第一个示例 一.环境要求 二.浏览器要求 三.安装node.js 四.下载cesium包(地址为https://cesiumjs.org) 包括了 五.在你的项目里引入相关js与cs ...