vue-路由使用
路由安装
终端下载路由插件 npm install vue-router --save-dev
配置
在main.js中引入插件
- //Router 为自定义名 vue-router 为插件的名字
- import Vue from 'vue'
- import Router from 'vue-router'
注册使用
- //注册使用 vue-router
- Vue.use(Router)
配置路由
- const router = new Router({
- routes : [
- {path : "/" ,name : "home" component: Home},
- {path : "/helloworld" , name : "helloworld",component: HelloWorld}
- ],
- mode : "history" //去掉路由地址后面的#
- })
- //可以将routes单独抽离出来
- const routes = [
- {
- path : "/" , //设置路由路径
- name : "home",
- component: Home // home在main.js里面注册的组件名,将要跳转到的组件
- },
- {path : "/helloworld" , name : "helloworld",component: HelloWorld},
- {path : "*" , redirect: '/'} //当点击不存在路径时 默认 跳转到路径 '/'
- ]
- const router = new Router({
- routes,
linkActiveClass: 'class' //设置路由比标签选中颜色- mode : "history"
- })
需要在main.js中 vue实例中引入router
- new Vue({
- router,
- el: '#app',
- components: { App },
- template: '<App/>'
- })
二级路由的设置
- //二级路由在当前路由下添加 children数组
- const routes = [
- {
- path : "/about" , //设置路由路径
- name:'about',
- component: About,
- redirect: '/about1', //设置当前页面默认显示的子路由
- children:[ //二级路由
- {path : "/about1" ,name:'about1', component: About1},
- {path : "/about2" ,name:'about2', component: About2},
- {path : "/about3" ,name:'about3', component: About3},
- {path : "/about3" ,name:'about3', component: About3},
- ]
- },
- ]
- const router = new Router({
- routes,
- mode : "history"
- })
跳转的几种写法
标签<router-link to=""></router-link>
一、
- <!-- to属性里面填入路径(在main.js中设置的路径) -->
2 <!-- tag属性可以设置 标签的展示 tag="div"标签是以div形式的展示 -->
- <router-link tag="div" to="/home">首页</router-link>
二、
- <!-- 通过 注册路由时设置的 name 属性 -->
- <router-link :to="{name : 'home'}">首页</router-link>
- 通过name属性
- {path : "/home" ,name:'home', component: Home},
三、
- <!-- 可以动态设置路径 -->
- <router-link :to="home">新闻</router-link>
- <!-- 可以通过data动态设置路径 -->
- data(){
- name : '/home'
- }
通过name属性实现<router-link></router-link>的复用
- <router-view name="home1"></router-view>
- <router-view name="home2"></router-view>
- <router-view name="home3"></router-view>
- const routes = [
- {
- path : "/" ,
- name:'home',
- components: { //components 这里有 s
- default : Home, // 当前的home页面
- //为复用的 路由设置 属性名
- 'home1' : Home1,
- 'home2' : Home2,
- 'home3' : Home3,
- }
- },
- ]
vue-路由使用的更多相关文章
- Vue路由vue-router
前面的话 在Web开发中,路由是指根据URL分配到对应的处理程序.对于大多数单页面应用,都推荐使用官方支持的vue-router.Vue-router通过管理URL,实现URL和组件的对应,以及通过U ...
- 攻克vue路由
先下手 路由是个好功能,但是每次都感觉没法开始下手,愣愣的看半天官方文档,所以做个快速开始教程. 首先先搭好HTML文件结构: <!--link和view在一个父元素下--> <di ...
- Vue路由学习心得
GoodBoy and GoodGirl~进来了就看完点个赞再离开,写了这么多也不容易的~ 一.介绍 1.概念:路由其实就是指向的意思,当我们点击home按钮时,页面中就要显示home的内容,点击l ...
- VUE路由新页面打开的方法总结
平常做单页面的场景比较多,所以大部分的业务是在同一个页面进行跳转.要通过VUE路由使用新页面打开且传递参数,可以采用以下两个方法: 1.router-link的target <router-li ...
- vue路由参数变化刷新数据
当路由到某个组件时,由于组件会复用,所以生命周期函数不会再次执行, 如果这个组件是模板组件,靠传入不同数据来显示的.那么,可能会发生参数变化了但页面数据却不变化. 问题 假如有个组件 info.vue ...
- 10.3 Vue 路由系统
Vue 路由系统 简单示例 main.js import Vue from 'vue' import App from './App.vue' //https://router.vuejs.or ...
- vue路由原理剖析
单页面应用(SPA)的核心之一是: 更新视图而不重新请求页面, 实现这一点主要是两种方式: 1.Hash: 通过改变hash值 2.History: 利用history对象新特性(详情可出门左拐见: ...
- 3种vue路由传参的基本模式
路由是连接各个页面的桥梁,而参数在其中扮演者异常重要的角色,在一定意义上,决定着两座桥梁是否能够连接成功. 在vue路由中,支持3中传参方式. 场景,点击父组件的li元素跳转到子组件中,并携带参数,便 ...
- 14.vue路由&脚手架
一.vue路由:https://router.vuejs.org/zh/ 1.定义 let router = new VueRouter({ mode:"history/hash" ...
- react router @4 和 vue路由 详解(八)vue路由守卫
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 13.vue路由守卫 a.beforeEach 全局守卫 (每个路由调用前都会触发,根据 ...
随机推荐
- F - True Liars 带权并查集
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...
- [bzoj2599][IOI2011]Race_树上点分治
Race bzoj-2599 题目大意:询问一颗树上最短的.长度为k的链,边有边权,n个节点. 注释:$1\le n \le 2\cdot 10^5$,$1\le k \le 10^6$. 想法:树上 ...
- [MongoDB]mongo命令行工具
1.use dbname 自动创建 2.db.user.find() 空 show collections 空 show dbs 3.db.user.save({name:'',age:20}) db ...
- 优秀软件project师必备的7大特性
不是每个程序猿都能成为优秀的软件project师. 在过去的6年时间里,我在Ooyala.Quora和now Quip这3个创业公司面试过许很多多挺有发展潜力的"种子选手".他们都 ...
- 《Effective C++ 》学习笔记——条款12
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- 【字符串处理】AC自动机知识点&代码
代码: #include<iostream> #include<vector> #include<cstdio> #include<queue> #in ...
- 使用playonlinux安装windows软件
转载 http://qspy.is-programmer.com/posts/40913.html Wine提供了一个用来运行Windows程序的平台.PlayOnLinux 是使用 Python 写 ...
- [RK3288][Android6.0] 调试笔记 --- 系统第一次开机进入Recovery模式原因【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/53464461 latform: ROCKCHIPOS: Android 6.0Kernel: ...
- 洛谷P1040 加分二叉树(区间dp)
P1040 加分二叉树 题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di, ...
- Spring MVC中传递json数据时显示415错误解决方法
在ajax中设置 ContentType为'application/json;charset=utf-8' 传递的data类型必须是json字符串类型:{“key”:"value" ...