一、SPA中路由的简单实现

main.js

import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'
import Page01 from './components/page01'
import Page02 from './components/page02' Vue.use(VueRouter)//全局安装路由功能
//定义路径
const routes = [
{ path: '/', component: Page01 },
{ path: '/02', component: Page02 },
]
//创建路由对象
const router = new VueRouter({
routes
}) new Vue({
el: '#app',
template: '<App/>',
components: { App },
router
})

App.vue

<template>
<div id="app">
<router-link to="/">01</router-link>
<router-link to="/02">02</router-link>
<br/>
<router-view></router-view>
</div>
</template>

page01.vue

<template>
<div>
<h1>page01</h1>
</div>
</template>

page02.vue

<template>
<div>
<h1>page02</h1>
</div>
</template>

实现步骤:

1.npm安装vue-router
2.Vue.use(VueRouter)全局安装路由功能
3.定义路径数组routes并创建路由对象router
4.将路由注入到Vue对象中
5.在根组件中使用<router-link>定义跳转路径
6.在根组件中使用<router-view>来渲染组件
7.创建子组件

二、路由的跳转

router-link

router-link 标签用于页面的跳转

<router-link to="/page01">page01</router-link>

点击这个router-link标签router-view就会渲染路径为/page01的页面。
注意:router-link默认是一个a标签的形式,如果需要显示不同的样子,可以在router-link标签中写入不同标签元素,如下显示为button按钮。

<router-link to="/04">
<button>to04</button>
</router-link>

router.push

下面我们通过JS代码控制路由的界面渲染,官方是写法如下:

// 字符串
router.push('home')
// 对象
router.push({ path: 'home' })
// 命名的路由
router.push({ name: 'user', params: { userId: 123 }})
// 带查询参数,变成 /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})

那么问题来了,如果是全局注册的路由Vue.use(VueRouter),应该怎么写呢?

// 字符串
this.$router.push('home')
// 对象
this.$router.push({ path: 'home' })
// 命名的路由
this.$router.push({ name: 'user', params: { userId: 123 }})
// 带查询参数,变成 /register?plan=private
this.$router.push({ path: 'register', query: { plan: 'private' }})

push方法其实和<router-link :to="...">是等同的。
注意:push方法的跳转会向 history 栈添加一个新的记录,当我们点击浏览器的返回按钮时可以看到之前的页面。

router.replace

push方法会向 history 栈添加一个新的记录,而replace方法是替换当前的页面,不会向 history 栈添加一个新的记录。用法如下
template

<router-link to="/05" replace>05</router-link>

script

this.$router.replace({ path: '/05' })

router.go

go方法用于控制history记录的前进和后退

// 在浏览器记录中前进一步,等同于 history.forward()
this.$router.go(1)
// 后退一步记录,等同于 history.back()
this.$router.go(-1)
// 前进 3 步记录router.go(3)
// 如果 history 记录不够用,那就默默地失败呗
this.$router.go(-100)
this.$router.go(100)

其实很好理解:go方法就是浏览器上的前进后退按钮,后面的参数就是前进和后退的次数

三、路由的传参方式

在路由跳转的过程中会传递一个object,我们可以通过watch方法查看路由信息对象。

watch: {
'$route' (to, from) {
console.log(to);
console.log(from);
},
},

console中看到的路由信息对象

{
...
params: { id: '123' },
query: { name: 'jack' },
...
}

这两个参数会在页面跳转后写在路径中,路径相当于/page/123?name=jack

1. params

params传递的数据可用于匹配动态路由字段。如params的数据为 params: { abc: 'hello', txt: 'world' } 而动态路由路径为 path: '/05/:txt 那么最终的路径就会是 /05/world所以,动态路由其实就是一种params的传递方式。

注意:由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效!需要用name来指定页面。之后动态路由会从params中找到动态路由同名的数据。

传递数据

在路由配置文件中定义参数

export default [
...
{ name: 'Page05', path: '/05/:txt', component: Page05 },
]

下面有两种传递params的方式

1. 通过path传递

路径后面的/:txt就是我们要传递的参数。

this.$router.push({ path: '/05/441'})

此时路由跳转的路径

http://localhost:8080/#/05/441

此时我们看到查看路由信息对象:

{
...
params: {
txt: '441'
}
...
}

2. 通过params传递

this.$router.replace({
name: 'Page05',
params: { abc: 'hello', txt: 'world' },
query: { name: 'query', type: 'object' }
})

通过name获取页面,传递params和query。
得到的URL为

http://localhost:8080/#/05/world?name=query&type=object

而获取到的参数为

{
...
params: {
abc: "hello",
txt: "world"
}
...
}

获取数据

template

<h2> {{ $route.params.txt }} </h2>

script

console.log(this.$route.params.txt)

3. query

query传递数据的方式就是URL常见的查询参数,如/foo?user=1&name=2&age=3。很好理解,下面就简单写一下用法以及结果

传递数据

template

<router-link :to="{ path: '/05', query: { name: 'query', type: 'object' }}" replace>05</router-link>

script

this.$router.replace({ path: '/05', query: { name: 'query', type: 'object' }})

路径结果

http://localhost:8080/#/05?name=query&type=object

路由信息对象

{
...
query: {
name: "query",
type: "object"
}
...
}

获取数据

获取数据和params是一样的。
template

<h2> {{ $route.query.name }} </h2>

script

console.log(this.$route.query.type)

.

vue2.0 vue-router的更多相关文章

  1. Vue 2.0 + Vue Router + Vuex

    用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...

  2. vue2.0 vue.set()

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. vue2.0 vue.extend()的拓展

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Vue2.0 Vue.set的使用

    原文链接: https://blog.csdn.net/qq_30455841/article/details/78666571

  5. vue1.0和vue2.0的区别(二)

    这篇我们继续之前的vue1.0和vue2.0的区别(一)继续说 四.循环 学过vue的同学应该知道vue1.0是不能添加重复数据的,否则它会报错,想让它重复添加也不是不可以,不过需要定义别的东西 而v ...

  6. VUE2.0学习总结

    摘要: 年后公司项目开始上vue2.0,自己对学习进行了总结,希望对大家有帮助! VUE2.0学习 vue介绍 vue是什么? https://vuefe.cn/guide vue也是一个数据驱动框架 ...

  7. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十二║Vue实战:个人博客第一版(axios+router)

    前言 今天正式开始写代码了,之前铺垫了很多了,包括 6 篇基础文章,一篇正式环境搭建,就是为了今天做准备,想温习的小伙伴可以再看看<Vue 基础入门+详细的环境搭建>,内容很多,这里就暂时 ...

  8. vue2.0实践 —— Node + vue 实现移动官网

    简介 使用 Node + vue 对公司的官网进行了一个简单的移动端的实现. 源码 https://github.com/wx1993/node-vue-fabaocn 效果 组件 轮播图(使用 vu ...

  9. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十三║Vue实战:Vuex 其实很简单

    前言 哈喽大家周五好,马上又是一个周末了,下周就是中秋了,下下周就是国庆啦,这里先祝福大家一个比一个假日嗨皮啦~~转眼我们的专题已经写了第 23 篇了,好几次都坚持不下去想要中断,不过每当看到群里的交 ...

  10. vue2.0 如何自定义组件(vue组件的封装)

    一.前言 之前的博客聊过 vue2.0和react的技术选型:聊过vue的axios封装和vuex使用.今天简单聊聊 vue 组件的封装. vue 的ui框架现在是很多的,但是鉴于移动设备的复杂性,兼 ...

随机推荐

  1. RF操作滚动条(竖拉)

    方式一:window.scrollBy(0, document.body.scrollHeight) 方式二:window.scrollTo(0, document.body.scrollHeight ...

  2. oracle 多表连接查询

    一.内连接(inner join (可简写为join)) 内连接查询操作列出与连接条件匹配的数据行,它使用比较运算符比较被连接列的列值. 1.等值连接:在连接条件中使用等于号(=)运算符比较被连接列的 ...

  3. Python学习-day9 线程

    这节内容主要是关于线程的学习 首先要了解的什么是进程,什么是线程 进程与线程 什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称 ...

  4. foreach 、 for 换成 ForEach

    List<tBaseRolerMapPower> list =new  List<tBaseRolerMapPower>() string strName = string.E ...

  5. html 标签附加文本属性

    <!DOCTYPE html> <html> <head> <script> function showDetails(animal) { var an ...

  6. Leetcode 472.连接词

    连接词 给定一个不含重复单词的列表,编写一个程序,返回给定单词列表中所有的连接词. 连接词的定义为:一个字符串完全是由至少两个给定数组中的单词组成的. 示例: 输入: ["cat" ...

  7. [错误解决]刚拿到的服务器vim退格键(backspace)失灵

    刚拿到的服务器vim退格键(backspace)失灵: 解决方案: 在主目录下建立.vimrc 覆盖/etc/vimrc的配置 .vimrc 与 /etc/vimrc的区别: 在启动的时候vim会读取 ...

  8. Etcd和ZooKeeper,究竟谁在watch的功能表现更好?

    ZooKeeper和Etcd的主要异同可以参考这篇文章,此外,Etcd的官网上也有对比表格(https://coreos.com/etcd/docs/latest/learning/why.html) ...

  9. 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)

    题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program ...

  10. 兼容ie8 rgba()写法

    background: rgba(255,255,255,.1); filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#1 ...