Vue-router官网

安装

vue-router是一个插件包,所以我们还是需要用npm 来进行安装。打开命令行工具,进入你的项目目录,输入下面命令。

npm install vue-router --save

如果在一个模块化工程中使用它。必须通过Vue.use()明确地安装路由功能

注意:先引入vue,再引入vue-router

import Vue from 'vue'
import VueRouter from 'vue-router' //显示声明使用VueRouter
Vue.use(VueRouter);

起步

  1. 创建一个vue-cli程序 参考链接

  2. 安装 参考安装标题

  3. 新建组件src/components/Content.vue和Main.vue

Content.vue

<template>
<h1>内容</h1>
</template> <script>
export default {
name: "Content"
}
</script> <style scoped> </style>

Main.vue

<template>
<h1>主页</h1>
</template> <script>
export default {
name: "Main"
}
</script> <style scoped> </style>
  1. 在src下新建一个文件router/index.js,进行创建路由和路由映射

默认情况下,我们进入一个网站的首页,应该先把首页渲染。所以可以设置默认路径

import Vue from 'vue'
import VueRouter from 'vue-router'
//导入上面自定义的组件
import Content from "../components/Content";
import Main from "../components/Main";
//安装路由
Vue.use(VueRouter); //配置导出路由
export default new VueRouter({
routes:[
{
path: '/',
redirect: '/main'
},
{
//路由路径
path:'/content',
//跳转的组件
component:Content
},
{
//路由路径
path:'/main',
//跳转的组件
component:Main
},
]
});
  1. 在main.js中挂载路由
import Vue from 'vue'
import App from './App.vue'
import router from "./router"
Vue.config.productionTip = false new Vue({
render: h => h(App),
router
}).$mount('#app')
  1. 在App.vue中使用路由
<template>
<div id="app">
<img src="./assets/logo.png">
<HelloWorld/>
<router-link to="/main">首页</router-link>
<router-link to="/content">内容</router-link>
<router-view></router-view>
</div>
</template>
....
  1. 实现效果

我们会发现URL中存在#,那怎么消除呢?

localhost:8081/#/content

使用HTML5的history模式

默认情况下,路径的改变使用的是URL的hash,如果我们希望使用H5的History模式,可以进行如下配置mode: 'history'

import Vue from 'vue'
import VueRouter from 'vue-router'
//导入上面自定义的组件
import Content from "../components/Content";
import Main from "../components/Main";
//安装路由
Vue.use(VueRouter); //配置导出路由
export default new VueRouter({
routes:[
{
path: '/',
redirect: '/main'
},
{
//路由路径
path:'/content',
//跳转的组件
component:Content
},
{
//路由路径
path:'/main',
//跳转的组件
component:Main
},
],
mode: 'history'
});

编程式导航

我们使用router-link的to属性,可以直接跳转路由。但是有时候,我们需要执行一段js代码之后,再跳转路由,这时候可以使用编程式导航。

语法:

This.$router.push(‘路由url’)

<button @click="toHome">首页</button>
methods: {
toHome() {
console.log('我将要跳转到首页')
this.$router.push('/home')
}
}

嵌套路由

比如在home页中,我们希望通过/home/news这种形式访问一些组件,在home页的基础上,进行组件展示,这时候可以使用嵌套路由。

实现嵌套路由有两个步骤。

  1. 创建对应的子组件,并且在路由配置中配置子路由。

  2. 在组件内部使用<router-view>组件

const routes = [
{
path: '/',
redirect: '/home'
},
{
path: '/home',
component: Home,
children: [ // 配置子路由
//默认路径news
{
path: '',
redirect:'news'
},
{
path: 'news',
component: () => import('../components/news')
},
{
path: 'message',
component: () => import('../components/message')
}
]
},
{
path: '/about',
component: () => import('../components/about')
}

路由传参

路由跳转可进行参数传递,参数传递分为两种:paramsquery

  • params的类型:

配置路由格式:/news/:id

这样就表示,我们在使用news的时候,需要在后面携带id参数。传递参数后的url:/news/123

  • query类型

配置路由方式不变。在路由后面使用?key=value传递

router-link传参

```vue

新闻2

```

JS代码传参

 toNews() {
this.$router.push({
path: '/home/news',
query: {name: '李四', age: 24}
})
}

获取参数

获取参数通过$route对象获取的。

created() {
console.log(this.$route.params)
console.log(this.$route.query)
}

router和route

$route$router是有区别的

$router是VueRouter实例,想要导航到不同的URL,使用$router.push方法。

$route是当前router的跳转对象,可以获取name、path等等

【vue-07】vue-router的更多相关文章

  1. 【前端面试】Vue面试题总结(持续更新中)

    Vue面试题总结(持续更新中) 题目参考链接 https://blog.csdn.net/weixin_45257157/article/details/106215158 由于已经有很多前辈深造VU ...

  2. 【非专业前端】Vue UI 之 建立Vuetify工程

    先建立一个工程[Webpack] .. ..建立好之后,进入目录,添加vuetify插件 cd vuetify-demo vue add vuetify[会出错] npm install vuetif ...

  3. 【转存】Vue组件选项props

    原帖地址 前面的话 组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项.在 Vue 中,父子组件的关系可以总结为 props down, events up.父组件通过  ...

  4. 【vue开发】vue导出Excel表格教程&demo

    前端工作量最多的就是需求,需求就是一直在变,比如当前端数据写完之后,需要用Excel把数据下载出来:再比如前端在没有数据库想写些demo玩时,也是很好的选择. 第一步安装依赖包,修改配置 1.装依赖: ...

  5. 【Vue课堂】Vue.js 父子组件之间通信的十种方式

    这篇文章介绍了Vue.js 父子组件之间通信的十种方式,不管是初学者还是已经在用 Vue 的开发者都会有所收获.无可否认,现在无论大厂还是小厂都已经用上了 Vue.js 框架,简单易上手不说,教程详尽 ...

  6. 8.【nuxt起步】-vue组件之间数据交互

    那么现在问题来了,我现在是在index.vue获取了服务端的数据,怎么传值到maincontent.vue?当然你也可以把获取数据放在maincontent.vue,但假如有些数据同时在header, ...

  7. 【15.07%】【codeforces 625A】Guest From the Past

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  8. 【42.07%】【codeforces 558A】Lala Land and Apple Trees

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【17.07%】【codeforces 583D】Once Again...

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  10. 【07】 vue 之 Vue-router

    注意: vue-router@2.x 只适用于 Vue 2.x 版本. vue-router@1.x 对应于Vue1.x版本. 的Github地址:vue-router 文档地址 7.2. vue-r ...

随机推荐

  1. Java流程控制:三种基本结构

    顺序结构: Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行顺序结构是最简单的算法结构语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的, ...

  2. 推荐一款小众且好用的 Python 爬虫库 - RoboBrowser

    1. 前言 大家好,我是安果! 今天推荐一款小众轻量级的爬虫库:RoboBrowser RoboBrowser,Your friendly neighborhood web scraper!由纯 Py ...

  3. Python3中变量作用域nonlocal的总结

    最近,在工作中踩到了一个关于Python3中nonlocal语句指定的变量作用域的坑.今天趁周六休息总结记录一下. 众所周知,Python中最常见的作用域定义如下:   但是,为了更加方便地在闭包函数 ...

  4. Hive数据导出的几种方式

    在hive的日常使用中,经常需要将hive表中的数据导出来,虽然hive提供了多种导出方式,但是面对不同的数据量.不同的需求,如果随意就使用某种导出方式,可能会导致导出时间过长,导出的结果不满足需求, ...

  5. 【python+selenium的web自动化】- PageObject模式解析及案例

    如果想从头学起selenium,可以去看看这个系列的文章哦! https://www.cnblogs.com/miki-peng/category/1942527.html PO模式 ​ Page O ...

  6. linux中c语言编程main函数和参数

    linux下main函数的的标准调用函数的标准形式 int main(int char,char *argv[]) 在main函数的两个参数中,argc必须是整型变量,其是命令行的参数的数目,argv ...

  7. c# DataGirdView动态刷新

    using MySql.Data.MySqlClient;using System; using System.Data; using System.Threading; using System.W ...

  8. 图解 | 原来这就是 class

    我是一个 .java 文件,名叫 FlashObject.java,叫我小渣就行. public class FlashObject {    private String name;    priv ...

  9. java进阶(40)--wait与notify(生产者与消费者模式)

    文档目录: 一.概念 二.wait的作用 三.notify的作用 四.生产者消费者模式 五.举例 ---------------------------------------分割线:正文------ ...

  10. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...