vue-router路由常用配置

1、mode:配置路由模式,默认为hash,由于URL很丑,可以修改为history,但是需要服务端的支持;

以上一篇的博文为实例:

初始时url的显示:

使用mode之后的显示:

使用mode的代码:

// 3 创建路由对象
const myRouter = new VueRouter({
//routes : routes
routes : myRoutes,
mode:'history'
});

但是当复制该链接在新的窗口打开的时候,不能打开该链接,如图:

说明需要服务端的支持

2、redirect:重定向,可以设置默认页面;

初始时,默认页面是没有显示的如图:

使用 redirect重定向后:

使用redirect的代码:

//2 配置路由 路由可能有多个
const myRoutes = [
{
path : "/home",
component : Home
},
{
path : "/foods",
component : Foods
},
{
path:"*",
redirect:"/home"
}
]

3、linkActiveClass:设置router-link激活样式;

由于router-link被渲染成为a标签:

有class,因此可以设置其点击时的样式:

修改该样式的css:

<style>

        .router-link-active{
color: white; background-color: black;
}
</style>

也可以通过 linkActiveClass:样式名称 进行设置其样式

代码如下,效果图同上:

// 3 创建路由对象
const myRouter = new VueRouter({
//routes : routes
routes : myRoutes,
//mode:'history'
linkActiveClass : "active" }); new Vue({
//router : router
router : myRouter //4 注入路由 简写
}).$mount("#one");
</script>
<style> .active{
color: white; background-color: black;
}
</style>

路由嵌套

路由的嵌套:一个路由中嵌套(包含)其他的路由;

在路由的配置中,使用children可以配置子路由,children也可以配置多个,与routes一致;

在上面的实例中的美食下添加几个路由,添加的路由就是它的子路由:

初始只配置了它的跳转,并没有配置它的路由

初始时代码:

<template id="foods">

        <div>

            <h2>美食广场</h2>
<ul>
<li><router-link to="/foods/bjc"> 北京菜</router-link></li>
<li><router-link to="/foods/hnc"> 湖南菜</router-link></li>
<li><router-link to="/foods/xc"> 湘菜</router-link></li>
<li><router-link to="/foods/yc"> 粤菜</router-link></li>
<li><router-link to="/foods/scc"> 四川菜</router-link></li>
</ul>
</div>
</template>
let Foods = {
template : "#foods"
}

定义路由后:

<template id="foods">

        <div>

            <h2>美食广场</h2>
<ul>
<router-link to="/foods/bjc" tag="li"> 北京菜</router-link>
<router-link to="/foods/hnc" tag="li"> 湖南菜</router-link>
<router-link to="/foods/xc" tag="li"> 湘菜</router-link>
<router-link to="/foods/yc" tag="li"> 粤菜</router-link>
<router-link to="/foods/scc" tag="li"> 四川菜</router-link>
</ul> <router-view></router-view>
</div>
</template> <script type="text/javascript" src="../js/vue.js" ></script>
<script type="text/javascript" src="../js/vue-router.js" ></script>
<script> //1 定义组件
let Home = {
template : "<h2>首页</h2>"
}
let Foods = {
template : "#foods"
} //定义foods中的子组件 let Bjc={
template : "<h3>北京菜</h3>" } let Hnc={
template : "<h3>湖南菜</h3>" }
let Xc={
template : "<h3>湘菜</h3>" } let Yc={
template : "<h3>粤菜</h3>" } let Scc={
template : "<h3>四川菜</h3>" } //2 配置路由 路由可能有多个
const myRoutes = [
{
path : "/home",
component : Home
},
{
path : "/foods",
component : Foods, children:[
{
path:"bjc",
component:Bjc },
{
path:"hnc",
component:Hnc }, {
path:"xc",
component:Xc },
{
path:"yc",
component:Yc },
{
path:"scc",
component:Scc } ]
},
{
path:"*",
redirect:"/home"
}
]

使用tag标签可以将router-link渲染成为li标签:

以上实例的所有代码:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> 路由的嵌套</title>
</head>
<body>
<div id="one">
<router-link to="/home">首页</router-link>
<router-link to="/foods">美食</router-link> <div>
<!--将数据显示在这里-->
<router-view></router-view>
</div>
</div>
</body>
<template id="foods"> <div> <h2>美食广场</h2>
<ul>
<router-link to="/foods/bjc" tag="li"> 北京菜</router-link>
<router-link to="/foods/hnc" tag="li"> 湖南菜</router-link>
<router-link to="/foods/xc" tag="li"> 湘菜</router-link>
<router-link to="/foods/yc" tag="li"> 粤菜</router-link>
<router-link to="/foods/scc" tag="li"> 四川菜</router-link>
</ul> <router-view></router-view>
</div>
</template> <script type="text/javascript" src="../js/vue.js" ></script>
<script type="text/javascript" src="../js/vue-router.js" ></script>
<script> //1 定义组件
let Home = {
template : "<h2>首页</h2>"
}
let Foods = {
template : "#foods"
} //定义foods中的子组件 let Bjc={
template : "<h3>北京菜</h3>" } let Hnc={
template : "<h3>湖南菜</h3>" }
let Xc={
template : "<h3>湘菜</h3>" } let Yc={
template : "<h3>粤菜</h3>" } let Scc={
template : "<h3>四川菜</h3>" } //2 配置路由 路由可能有多个
const myRoutes = [
{
path : "/home",
component : Home
},
{
path : "/foods",
component : Foods, children:[
{
path:"bjc",
component:Bjc },
{
path:"hnc",
component:Hnc }, {
path:"xc",
component:Xc },
{
path:"yc",
component:Yc },
{
path:"scc",
component:Scc } ]
},
{
path:"*",
redirect:"/home"
}
] // 3 创建路由对象
const myRouter = new VueRouter({
//routes : routes
routes : myRoutes,
//mode:'history'
linkActiveClass : "active" }); new Vue({
//router : router
router : myRouter //4 注入路由 简写
}).$mount("#one");
</script>
<style> .active{
color: white; background-color: black;
}
</style>
</html>

路由嵌套

Vue-Router路由 Vue-CLI脚手架和模块化开发 之 路由常用配置与路由嵌套的更多相关文章

  1. [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制

    一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...

  2. Vue-Router路由 Vue-CLI脚手架和模块化开发 之 使用路由对象获取参数

    使用路由对象$route获取参数: 1.params: 参数获取:使用$route.params获取参数: 参数传递: URL传参:例 <route-linke to : "/food ...

  3. Vue-Router路由Vue-CLI脚手架和模块化开发 之 vue-router路由

    vue-router路由:Vue.js官网推出的路由管理器,方便的构建单页应用: 单页应用(SPA)只有一个web页面的应用,用户与应用交互时,动态更新该页面的内容:简单来说,根据不同的url与数据, ...

  4. Vue-Router路由Vue-CLI脚手架和模块化开发 之 使用props替代路由对象的方式获取参数

    在上一章博文中使用路由对象$route获取参数时,组件和路由对象耦合,在这篇博文中就可以使用props来进行解耦: 1.在组件中使用props选项定义数据,接收参数: 2.在路由中,使用props选项 ...

  5. Vue-Router路由Vue-CLI脚手架和模块化开发 之 路由的动态跳转

    在上一篇的博文中,实现的跳转是在页面中进行实现的 利用vue-router实例方法,使用js对路由进行动态跳转: 1.router.push:参数为路由对象,跳转到指定路由,跳转后会产生历史记录: & ...

  6. 模块化开发RequireJS之shim配置

    一.shim requirejs使用AMD规范开发,若开发过程中加载非AMD规范js,需要使用requirejs的shim进行配置. shim配置语法为: //配置文件 requirejs.confi ...

  7. vue 使用cli脚手架手动创建项目 相关的选择配置及真正项目的开始

    转载https://www.jianshu.com/p/635bd3ab7383 根据上述连接将基本的环境和命令和装好 使用命令行  vue create 项目名称  出现选项  选择手动(没有截图展 ...

  8. Javascript模块化开发4——Grunt常用模块

    一.copy 用于复制文件与目录. grunt-contrib-copy 二.eslint 检测代码的合理性. grunt-eslint 常见参数: 1.quiet 是否只显示errors.默认值fa ...

  9. Vue.js 2.x笔记:路由Vue Router(6)

    1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...

随机推荐

  1. disconf安装问题

    安装参考文档:https://blog.csdn.net/fengyao1995/article/details/66491226 主要说说遇到的几个问题 1.在步骤6构建的时候,用jdk1.8,进行 ...

  2. sqlmap常用渗透方法

    0X001 适用场景 1.获取了Mysql数据库root账号以及密码. 2.可以访问3306端口以及数据库. 0X002 扫描获取root账号的密码 通常有下面一些方法来获取root账号的密码 (1) ...

  3. mysql GROUP_CONCAT 查询某个字段(查询结果默认逗号拼接)

    Mysql 的 GROUP_CONCAT 函数默认将查询的结果用逗号拼接并返回一个字符串,如:李四,long,张三 1. 常用方式 select GROUP_CONCAT(user_name) use ...

  4. 微信小程序区分点击,长按事件

    在上代码之前,微信小程序点击事件,长按事件的触发顺序需要我们了解一下下 事务分类 touchstart:手指触摸 longtap:手指触摸后后,超过350ms离开 touchend:手指触摸动作结束 ...

  5. 神贴真开眼界:为什么很多人倡导重视能力和素质,但同时对学历有严格要求?——代表了上一场比赛的输赢,招聘成本很重要。如果上一场游戏失败了,尽量让自己成为当前群体的尖子。学历只是其中的一个作品而已,但学历代表了学生时代为之做出的牺牲。人群自有偏向集中性 good

    对于软件工程师职位,没学历没关系,如果真觉得自己才高八斗,请在简历里附上 github项目链接或者 appstore/google play上你的作品.如果学历比别人低,那么想必是把时间和精力用在了其 ...

  6. 发现了一个比较有意思的url参数

    今天登录阿里云发现需要二次验证了,手机号不是我的很麻烦,然后就看到有个手机app快捷登录的方式,点进去一看,链接地址是这样的http://qd.alibaba.com/onekey.htm?spm=0 ...

  7. 1.Qt字符编码

    1.给空间设置内容,有显示中文的,必须是utf-8编码: 2.从Qt得到的字符串,如果有中文,编码是utf-8,和Linux是一样的: 3.如果使用标准的C函数,如果有中文,是gbk编码: ANSI, ...

  8. afn3.0源码解析---AFURLRequestSerialization

    AFHTTPRequestSerialization: @方法1 - (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLSt ...

  9. java8新特性学习笔记链接

    https://blog.csdn.net/yitian_66/article/details/81010434

  10. vi命令设置行号

    1. :set nu :显示行号