路由

安装 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基础的更多相关文章

  1. [Vue 牛刀小试]:第十三章 - Vue Router 基础使用再探(命名路由、命名视图、路由传参)

    一.前言 在上一章的学习中,我们简单介绍了前端路由的概念,以及如何在 Vue 中通过使用 Vue Router 来实现我们的前端路由.但是在实际使用中,我们经常会遇到路由传参.或者一个页面是由多个组件 ...

  2. vue Router——基础篇

    vue--Router简介 vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用. vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路 ...

  3. Vue技术点整理-Vue Router

    路由 Vue Router 对于单页面应用来说,如果涉及到多个页面的话,就必须要使用到路由,一般使用官方支持的 vue-router 库 一,Vue Router 在项目中的安装引用 1,在页面中使用 ...

  4. vue Router——进阶篇

    vue Router--基础篇 1.导航守卫 正如其名,vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的 ...

  5. react router @4 和 vue路由 详解(一)vue路由基础和使用

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 1.vue路由基础和使用 a.大概目录 我这里建了一个router文件夹,文件夹下有in ...

  6. 「vue基础」一篇浅显易懂的 Vue 路由使用指南( Vue Router 上)

    大家好,今天的内容,我将和大家一起聊聊 Vue 路由相关的知识,如果你以前做过服务端相关的开发,那你一定会对程序的URL结构有所了解,我没记错的话也是路由映射的概念,需要进行配置. 其实前端这些框架的 ...

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

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

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

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

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

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

随机推荐

  1. Ti 949 配置 948 i2c

    如果不想用 远端 slave,只访问948   选 i2c pass through all =1  :i2c pass through =0/1 0x0c 0x17 0x9e 如果想用远端  sla ...

  2. spring+springmvc+mybatis框架搭建

    一.开发前准备 1)ecplise4.11.0 百度网盘:https://pan.baidu.com/s/1wO9_I52lp0mYNeNTdnj80w 提取码:booa 2)jdk1.6.0_45  ...

  3. 【Jquery】this和event.target的区别

    1.js中事件是会冒泡的,所以this是可以变化的,但event.target不会变化,它永远是直接接受事件的目标DOM元素: 2.this和event.target都是dom对象,如果要使用jque ...

  4. linux配置java环境变量(详细)(转)

    linux配置java环境变量(详细) 一. 解压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586. ...

  5. Qt 【遍历文件夹文件,为listwidgetItem设置图标】

    效果图如上,通过qt本身的api去获取文件夹中文件的信息并且将其显示到listwidget上.只遍历当前文件夹的文件, 若文件夹中的子文件夹含有符合筛选器的文件那么并不会一同筛选出来 参考百度:htt ...

  6. mysql-一行分隔成多行数据

    mysql将某个字段有分隔符号分隔成多行数据 SELECT a.id, a. NAME, substring_index( substring_index( a.name, ',', b.help_t ...

  7. Comet OJ - Contest #11 E ffort(组合计数+多项式快速幂)

    传送门. 题解: 考虑若最后的总伤害数是s,那么就挡板分配一下,方案数是\(C_{s-1}^{n-1}\). 那么问题在于总伤害数很大,不能一个一个的算. \(C_{s-1}^{n-1}\)的OGF是 ...

  8. 同源策略 - JSONP - CORS

    1.  Jquery 对象可以通过 .index() 进行取出自当前元素在父级元素中存放的索引: 2. 浏览器的同源策略 -- Ajax 在访问非本网站的时候,在数据返回的时候,会被浏览器拦截 - 后 ...

  9. Yii2 数据库Active Record(ORM)

    ACTIVE RECORD(ORM) 参考:http://www.yiiframework.com/doc-2.0/guide-db-active-record.html namespace app\ ...

  10. php linux下安装xml扩展

    1.进入PHP安装源码包,找到ext下的ftp,进入 cd /home/local/php-5.6.25/ext/xml 2./usr/local/php/bin/phpize 3../configu ...