路由安装

终端下载路由插件 npm install vue-router --save-dev

配置

在main.js中引入插件

  1. //Router 为自定义名 vue-router 为插件的名字
  2.  
  3. import Vue from 'vue'
  4.  
  5. import Router from 'vue-router'

注册使用

  1. //注册使用 vue-router
  2.  
  3. Vue.use(Router)

配置路由

  1. const router = new Router({
  2. routes : [
  3. {path : "/" ,name : "home" component: Home},
  4. {path : "/helloworld" , name : "helloworld",component: HelloWorld}
  5. ],
  6. mode : "history" //去掉路由地址后面的#
  7. })
  8.  
  9. //可以将routes单独抽离出来
  10.  
  11. const routes = [
  12. {
  13. path : "/" , //设置路由路径
  14. name : "home",
  15. component: Home // home在main.js里面注册的组件名,将要跳转到的组件
  16. },
  17.  
  18. {path : "/helloworld" , name : "helloworld",component: HelloWorld},
  19.  
  20. {path : "*" , redirect: '/'} //当点击不存在路径时 默认 跳转到路径 '/'
  21. ]
  22.  
  23. const router = new Router({
  24. routes,
      linkActiveClass: 'class' //设置路由比标签选中颜色
  25. mode : "history"
  26. })

需要在main.js中 vue实例中引入router 

  1. new Vue({
  2. router,
  3. el: '#app',
  4. components: { App },
  5. template: '<App/>'
  6. })

二级路由的设置

  1. //二级路由在当前路由下添加 children数组
  2.  
  3. const routes = [
  4. {
  5. path : "/about" , //设置路由路径
  6. name:'about',
  7. component: About,
  8. redirect: '/about1', //设置当前页面默认显示的子路由
  9. children:[ //二级路由
  10. {path : "/about1" ,name:'about1', component: About1},
  11. {path : "/about2" ,name:'about2', component: About2},
  12. {path : "/about3" ,name:'about3', component: About3},
  13. {path : "/about3" ,name:'about3', component: About3},
  14. ]
  15. },
  16. ]
  17.  
  18. const router = new Router({
  19. routes,
  20. mode : "history"
  21. })

跳转的几种写法

标签<router-link to=""></router-link>

一、

  1. <!-- to属性里面填入路径(在main.js中设置的路径) -->
    2 <!-- tag属性可以设置 标签的展示 tag="div"标签是以div形式的展示 -->

  2. <router-link tag="div" to="/home">首页</router-link>

二、

  1. <!-- 通过 注册路由时设置的 name 属性 -->
  2.  
  3. <router-link :to="{name : 'home'}">首页</router-link>
  4.  
  5. 通过name属性
  6. {path : "/home" ,name:'home', component: Home},

三、

  1. <!-- 可以动态设置路径 -->
  2.  
  3. <router-link :to="home">新闻</router-link>

  4. <!-- 可以通过data动态设置路径 -->
  5. data(){
  6. name : '/home'
  7. }

通过name属性实现<router-link></router-link>的复用

  1. <router-view name="home1"></router-view>
  2.  
  3. <router-view name="home2"></router-view>
  4.  
  5. <router-view name="home3"></router-view>
  1. const routes = [
  2. {
  3. path : "/" ,
  4. name:'home',
  5. components: { //components 这里有 s
  6. default : Home, // 当前的home页面
  7. //为复用的 路由设置 属性名
  8. 'home1' : Home1,
  9. 'home2' : Home2,
  10. 'home3' : Home3,
  11.  
  12. }
  13. },
  14. ]

vue-路由使用的更多相关文章

  1. Vue路由vue-router

    前面的话 在Web开发中,路由是指根据URL分配到对应的处理程序.对于大多数单页面应用,都推荐使用官方支持的vue-router.Vue-router通过管理URL,实现URL和组件的对应,以及通过U ...

  2. 攻克vue路由

    先下手 路由是个好功能,但是每次都感觉没法开始下手,愣愣的看半天官方文档,所以做个快速开始教程. 首先先搭好HTML文件结构: <!--link和view在一个父元素下--> <di ...

  3. Vue路由学习心得

    GoodBoy and GoodGirl~进来了就看完点个赞再离开,写了这么多也不容易的~ 一.介绍  1.概念:路由其实就是指向的意思,当我们点击home按钮时,页面中就要显示home的内容,点击l ...

  4. VUE路由新页面打开的方法总结

    平常做单页面的场景比较多,所以大部分的业务是在同一个页面进行跳转.要通过VUE路由使用新页面打开且传递参数,可以采用以下两个方法: 1.router-link的target <router-li ...

  5. vue路由参数变化刷新数据

    当路由到某个组件时,由于组件会复用,所以生命周期函数不会再次执行, 如果这个组件是模板组件,靠传入不同数据来显示的.那么,可能会发生参数变化了但页面数据却不变化. 问题 假如有个组件 info.vue ...

  6. 10.3 Vue 路由系统

     Vue 路由系统  简单示例 main.js  import Vue from 'vue' import App from './App.vue' //https://router.vuejs.or ...

  7. vue路由原理剖析

    单页面应用(SPA)的核心之一是: 更新视图而不重新请求页面, 实现这一点主要是两种方式: 1.Hash: 通过改变hash值 2.History: 利用history对象新特性(详情可出门左拐见:  ...

  8. 3种vue路由传参的基本模式

    路由是连接各个页面的桥梁,而参数在其中扮演者异常重要的角色,在一定意义上,决定着两座桥梁是否能够连接成功. 在vue路由中,支持3中传参方式. 场景,点击父组件的li元素跳转到子组件中,并携带参数,便 ...

  9. 14.vue路由&脚手架

    一.vue路由:https://router.vuejs.org/zh/ 1.定义 let router = new VueRouter({ mode:"history/hash" ...

  10. react router @4 和 vue路由 详解(八)vue路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 13.vue路由守卫 a.beforeEach 全局守卫 (每个路由调用前都会触发,根据 ...

随机推荐

  1. F - True Liars 带权并查集

    After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...

  2. [bzoj2599][IOI2011]Race_树上点分治

    Race bzoj-2599 题目大意:询问一颗树上最短的.长度为k的链,边有边权,n个节点. 注释:$1\le n \le 2\cdot 10^5$,$1\le k \le 10^6$. 想法:树上 ...

  3. [MongoDB]mongo命令行工具

    1.use dbname 自动创建 2.db.user.find() 空 show collections 空 show dbs 3.db.user.save({name:'',age:20}) db ...

  4. 优秀软件project师必备的7大特性

    不是每个程序猿都能成为优秀的软件project师. 在过去的6年时间里,我在Ooyala.Quora和now Quip这3个创业公司面试过许很多多挺有发展潜力的"种子选手".他们都 ...

  5. 《Effective C++ 》学习笔记——条款12

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  6. 【字符串处理】AC自动机知识点&代码

    代码: #include<iostream> #include<vector> #include<cstdio> #include<queue> #in ...

  7. 使用playonlinux安装windows软件

    转载 http://qspy.is-programmer.com/posts/40913.html Wine提供了一个用来运行Windows程序的平台.PlayOnLinux 是使用 Python 写 ...

  8. [RK3288][Android6.0] 调试笔记 --- 系统第一次开机进入Recovery模式原因【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/53464461 latform: ROCKCHIPOS: Android 6.0Kernel: ...

  9. 洛谷P1040 加分二叉树(区间dp)

    P1040 加分二叉树 题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di, ...

  10. Spring MVC中传递json数据时显示415错误解决方法

    在ajax中设置 ContentType为'application/json;charset=utf-8' 传递的data类型必须是json字符串类型:{“key”:"value" ...