vue学习十四(嵌套路由、命名路由、命名视图、重定向)

本文主要是介绍vue学习十四(嵌套路由、命名路由、命名视图、重定向),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

      • 嵌套路由
      • 命名路由
      • 命名视图
      • 重定向

嵌套路由

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>Title</title>
</head><body><div id="app"><router-link to="/green" tag="li">green</router-link><router-link to="/fruit" tag="li">fruit</router-link><router-view></router-view></div><template id="green"><div>蔬菜<router-link to="/green/organic">有机</router-link><router-link to="/green/inorganic">无机</router-link><router-view></router-view></div></template><template id="fruit"><div>水果<router-link to="/fruit/sweet">甜的</router-link><router-link to="/fruit/acid">酸的</router-link><router-view></router-view></div></template></body><script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script>
<script>//父路由 有蔬菜,水果,蔬菜里面有有机蔬菜,无机蔬菜 ,水果里面有甜的酸的let green = { template: '#green' }let organic = { template: "<div>有机蔬菜organic</div>" }let inorganic = { template: "<div>无机蔬菜inorganic</div>" }let fruit = { template: '#fruit' }let sweet = { template: "<div>甜的</div>" }let acid = { template: "<div>酸的</div>" }//路由路径映射表let routes = [//路由默认去的第一个组件{path: '',component: green},{path: "/green",component: green,children: [{path: "",component: organic},{path: "organic",component: organic},{path: "inorganic",component: inorganic}]},{path: "/fruit",component: fruit,children: [{path: "",component: sweet},{path: "sweet",component: sweet},{path: "acid",component: acid}]},//所有没有找到时候,去地址/green的组件{path: "*",redirect: '/green'}]//实例化一个路由let router = new VueRouter({routes})let vm = new Vue({el: "#app",data: {},router})
</script></html>

file:///Users/zhiliao/zhiliao/vue/index_test.html#/

green
fruit
蔬菜 有机 无机

file:///Users/zhiliao/zhiliao/vue/index_test.html#/green
file:///Users/zhiliao/zhiliao/vue/index_test.html#/green/organic

green
fruit
蔬菜 有机 无机
有机蔬菜organic

file:///Users/zhiliao/zhiliao/vue/index_test.html#/green/inorganic

green
fruit
蔬菜 有机 无机
无机蔬菜inorganic

file:///Users/zhiliao/zhiliao/vue/index_test.html#/fruit
file:///Users/zhiliao/zhiliao/vue/index_test.html#/fruit/sweet

green
fruit
水果 甜的 酸的
甜的

file:///Users/zhiliao/zhiliao/vue/index_test.html#/fruit/acid

green
fruit
水果 甜的 酸的
酸的

命名路由

有时候,通过一个名称来标识一个路由显得更方便一些,特别是在链接一个路由,或者是执行一些跳转的时候。你可以在创建 Router 实例的时候,在 routes 配置中给某个路由设置名称。


<body><div id="box"><router-link :to="{ name: 'foo'}">One</router-link><router-link :to="{ name: 'bar'}">Two</router-link><router-view></router-view></div><script>//1、定义 (路由) 模版组件const Foo = { template: '<div>第一个router</div>' }const Bar = { template: '<div>第二个router</div>' }//2、定义路由var routes = [{path: "/one",name: 'foo',component: Foo},{path: "/two",name: 'bar',component: Bar},];// 3、创建 router 实例var router = new VueRouter({routes});// 4、创建和挂载根实例const app = new Vue({router}).$mount('#box')</script>
</body>

效果图如下:

One Two
第一个router

命名视图

有时候想同时 (同级) 展示多个视图,而不是嵌套展示,例如创建一个布局,有 sidebar (侧导航) 和 main (主内容) 两个视图,这个时候命名视图就派上用场了。你可以在界面中拥有多个单独命名的视图,而不是只有一个单独的出口。如果 router-view 没有设置名字,那么默认为 default。

<router-view class="view one"></router-view>
<router-view class="view two" name="a"></router-view>
<router-view class="view three" name="b"></router-view>

我们来看一个例子:


<body><div id="box"><router-link to="/one">One</router-link><router-view name="a_view"></router-view></div><script>//1、定义 (路由) 模版组件const Foo = { template: '<div>第一个router</div>' }//2、定义路由var routes = [{path: "/one",// name: 'foo',components: {a_view: Foo,}}];// 3、创建 router 实例var router = new VueRouter({routes});// 4、创建和挂载根实例const app = new Vue({router}).$mount('#box')</script>
</body>

重定向

重定向也是通过 routes 配置来完成,下面例子是从 /a 重定向到 /b:

const router = new VueRouter({routes: [{ path: '/a', redirect: '/b' }]
})

重定向的目标也可以是一个命名的路由:

const router = new VueRouter({routes: [{ path: '/a', redirect: { name: 'foo' }}]
})

甚至是一个方法,动态返回重定向目标:

const router = new VueRouter({routes: [{ path: '/a', redirect: to => {// 方法接收 目标路由 作为参数// return 重定向的 字符串路径/路径对象}}]
})

我们来看一个例子:


<body><div id="box"><router-link to="/one">One</router-link><router-link to="/two">Two</router-link><router-view></router-view></div><!--定义模版--><template id="Foo"><div>第一个router</div></template><template id="Bar"><div>第二个router</div></template><script>//1、定义 (路由) 模版组件//2、定义路由var routes = [{path: '/oneone', redirect: '/one' },{path: "/one",component: { template: "#Foo" }},{path: "/two",component: { template: "#Bar" }},];// 3、创建 router 实例var router = new VueRouter({routes});// 4、创建和挂载根实例const app = new Vue({router}).$mount('#box')</script></body>

这篇关于vue学习十四(嵌套路由、命名路由、命名视图、重定向)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125126

相关文章

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

css中的 vertical-align与line-height作用详解

《css中的vertical-align与line-height作用详解》:本文主要介绍了CSS中的`vertical-align`和`line-height`属性,包括它们的作用、适用元素、属性值、常见使用场景、常见问题及解决方案,详细内容请阅读本文,希望能对你有所帮助... 目录vertical-ali

Spring MVC使用视图解析的问题解读

《SpringMVC使用视图解析的问题解读》:本文主要介绍SpringMVC使用视图解析的问题解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC使用视图解析1. 会使用视图解析的情况2. 不会使用视图解析的情况总结Spring MVC使用视图

浅析CSS 中z - index属性的作用及在什么情况下会失效

《浅析CSS中z-index属性的作用及在什么情况下会失效》z-index属性用于控制元素的堆叠顺序,值越大,元素越显示在上层,它需要元素具有定位属性(如relative、absolute、fi... 目录1. z-index 属性的作用2. z-index 失效的情况2.1 元素没有定位属性2.2 元素处

Python实现html转png的完美方案介绍

《Python实现html转png的完美方案介绍》这篇文章主要为大家详细介绍了如何使用Python实现html转png功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 1.增强稳定性与错误处理建议使用三层异常捕获结构:try: with sync_playwright(

Vue 调用摄像头扫描条码功能实现代码

《Vue调用摄像头扫描条码功能实现代码》本文介绍了如何使用Vue.js和jsQR库来实现调用摄像头并扫描条码的功能,通过安装依赖、获取摄像头视频流、解析条码等步骤,实现了从开始扫描到停止扫描的完整流... 目录实现步骤:代码实现1. 安装依赖2. vue 页面代码功能说明注意事项以下是一个基于 Vue.js

python展开嵌套列表的多种方法

《python展开嵌套列表的多种方法》本文主要介绍了python展开嵌套列表的多种方法,包括for循环、列表推导式和sum函数三种方法,具有一定的参考价值,感兴趣的可以了解一下... 目录一、嵌套列表格式二、嵌套列表展开方法(一)for循环(1)for循环+append()(2)for循环+pyPhWiFd

Pytorch微调BERT实现命名实体识别

《Pytorch微调BERT实现命名实体识别》命名实体识别(NER)是自然语言处理(NLP)中的一项关键任务,它涉及识别和分类文本中的关键实体,BERT是一种强大的语言表示模型,在各种NLP任务中显著... 目录环境准备加载预训练BERT模型准备数据集标记与对齐微调 BERT最后总结环境准备在继续之前,确

Linux命名管道方式

《Linux命名管道方式》:本文主要介绍Linux命名管道方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、命名管道1、与匿名管道的关系2、工作原理3、系统调用接口4、实现两个进程间通信二、可变参数列表总结一、命名管道1、与匿名管道的关系命名管道由mkf

CSS @media print 使用详解

《CSS@mediaprint使用详解》:本文主要介绍了CSS中的打印媒体查询@mediaprint包括基本语法、常见使用场景和代码示例,如隐藏非必要元素、调整字体和颜色、处理链接的URL显示、分页控制、调整边距和背景等,还提供了测试方法和关键注意事项,并分享了进阶技巧,详细内容请阅读本文,希望能对你有所帮助...