首页 > 其他分享 >vue--day82--命名路由

vue--day82--命名路由

时间:2023-09-11 23:23:58浏览次数:37  
标签:vue name title -- day82 nbsp pages

### 5.命名路由

 

1. 作用:可以简化路由的跳转。

 

2. 如何使用

 

   1. 给路由命名:

 

      ```js

      {

      path:'/demo',

      component:Demo,

      children:[

      {

      path:'test',

      component:Test,

      children:[

      {

                            name:'hello' //给路由命名

      path:'welcome',

      component:Hello,

      }

      ]

      }

      ]

      }

      ```

 

   2. 简化跳转:

 

      ```vue

      <!--简化前,需要写完整的路径 -->

      <router-link to="/demo/test/welcome">跳转</router-link>

      

      <!--简化后,直接通过名字跳转 -->

      <router-link :to="{name:'hello'}">跳转</router-link>

      

      <!--简化写法配合传递参数 -->

      <router-link 

      :to="{

      name:'hello',

      query:{

         id:666,

                  title:'你好'

      }

      }"

      >跳转</router-link>

      ```

 

 

1. App.vue

<template> <div> <Banner></Banner> <div class="row"> <div class="col-xs-2 col-xs-offset-2"> <div class="list-group">
<!--原始html 中我们使用a 标签 实现页面的跳转--> <!-- <a class="list-group-item active" href="./about.html">About</a> <a class="list-group-item" href="./home.html">Home</a> -->
<!--VUE 中借助router-link 标签实现路由的切换--> <router-link class="list-group-item" active-class="active" :to="{name:'guanyu'}">About</router-link> <router-link class="list-group-item" active-class="active" to="/home">Home</router-link>
</div> </div> <div class="col-xs-6"> <div class="panel"> <div class="panel-body"> <!-- 指定组件的呈现位置--> <!-- <h2>此处到底展示什么组件,得看用户点击哪个导航项</h2> --> <!-- <router-view></router-view> --> <router-view></router-view>
</div> </div> </div> </div> </div>     </template>   <script> import Banner from './components/Banner.vue' export default { name: 'App', components:{ Banner }   }
</script>
     2. pages/Home.vue <template>  
<div class="col-xs-6"> <div class="panel"> <div class="panel-body"> <div> <h2>Home组件内容</h2> <div> <ul class="nav nav-tabs"> <li> <router-link class="list-group-item " active-class="active" to="/home/news">News</router-link> </li> <li> <router-link class="list-group-item " active-class="active" to="/home/message">Message</router-link> </li> </ul>   <router-view></router-view> </div> </div> </div> </div> </div>     </template>
<script> export default { name:'Home', beforeDestroy(){ console.log("home组件即将被销毁了"); }, mounted(){ console.log("home组件挂载完毕了",this); window.homeRoute=this.$route; window.homeRouter=this.$router; } } </script>
<style>
</style> 3. pages/About.vue <template>  
<h2>我是About的内容</h2>     </template>
<script> export default { name:'About', beforeDestroy(){ console.log("about组件即将被销毁了"); }, mounted(){ console.log("about组件挂载完毕了",this); window.aboutRoute=this.$route; window.aboutRouter=this.$router; } } </script>
<style>
</style> 4. pages/News.vue   <template> <ul> <li>news001</li> <li>news002</li> <li>news003</li> </ul> </template>
<script> export default { name:'News'
} </script>
<style>
</style>   5. Message.vue <template> <div> <ul v-for="m in messageList" :key="m.id"> <li> <!--跳转 路由 并且携带query 参数 to 的字符串写法--> <!-- <router-link :to="`/home/message/detail?id=${m.id}&title=${m.title}`">{{ m.title }}</router-link>&nbsp;&nbsp; -->   <!--跳转 路由 并且携带query 参数 to 的对象写法--> <router-link :to="{ name:'xiangqing', // path:'/home/message/detail', query:{ id:m.id, title:m.title } }"> {{ m.title }} </router-link>&nbsp;&nbsp; </li> </ul>
<hr>
<router-view></router-view> </div> </template>
<script> export default { name:'Message', data(){ return{ messageList:[ {id:'001',title:'消息001'}, {id:'002',title:'消息002'}, {id:'003',title:'消息003'}, ] } } } </script>
<style>
</style> 6. pages/Detail.vue <template> <div> <ul v-for="m in messageList" :key="m.id"> <li> <!--跳转 路由 并且携带query 参数 to 的字符串写法--> <!-- <router-link :to="`/home/message/detail?id=${m.id}&title=${m.title}`">{{ m.title }}</router-link>&nbsp;&nbsp; -->   <!--跳转 路由 并且携带query 参数 to 的对象写法--> <router-link :to="{ name:'xiangqing', // path:'/home/message/detail', query:{ id:m.id, title:m.title } }"> {{ m.title }} </router-link>&nbsp;&nbsp; </li> </ul>
<hr>
<router-view></router-view> </div> </template>
<script> export default { name:'Message', data(){ return{ messageList:[ {id:'001',title:'消息001'}, {id:'002',title:'消息002'}, {id:'003',title:'消息003'}, ] } } } </script>
<style>
</style> 8. router/index.js //该文件专门用于创建整个应用的路由器 import VueRouter from "vue-router" import About from '../pages/About' import Home from '../pages/Home' import News from '../pages/News' import Message from '../pages/Message' import Detail from '../pages/Detail' // 创建并且暴露一个路由器 export default new VueRouter({ routes:[ { name:'guanyu', path:'/about', component:About }, { path:'/home', component:Home, children:[ { path:'news', component:News,
},
{   path:'message', component:Message, children:[ { name:'xiangqing', path:'detail', component:Detail, }  
]
},   ] },
] })

标签:vue,name,title,--,day82,nbsp,pages
From: https://www.cnblogs.com/satisfysmy/p/17694847.html

相关文章

  • 记录Mac下vscode无法附加到本地docker容器的问题
    我想用vscode直接附加到本地docker容器,在里面编辑开发,传统的办法要在容器内设置ssh服务器,通过vscode的remote插件先进入到容器里面,再编辑开发,可是我看似乎vscode做了优化,本地容器可以免去ssh服务,通过devcontainers插件和docker插件的AttachtorunningDockercontainer、在新窗......
  • BooleanQuery
         ......
  • 从源代码安装UE5.2
    总体上按照UE5在文档上源码编译流程进行安装,这里只说几个遇到的问题。出现MSB错误:查看log是否出现过warning,MSB错误可能是由于之前的其他问题导致的。在无其他问题的状况下,考虑是否是中文路径,或者路径长度突破了windows中260字符的限制。启动UE5.2后无法新建项目,输出错误为???......
  • MoeCTF 2023
    0x01text64v1可以由用户操控,直接造成栈溢出。同时程序里存在分开的system函数和"/bin/sh"字符串,所以就是简单的ret2libc1。Exploit为frompwnimport*o=process("./pwn")bin_sh=0x404050system_plt=0x401090pop_rdi=0x4011BEret=0x4012A4o.sendline(b"200")......
  • vmware 虚拟机上实现host与guest文件及目录互相复制粘贴
    往期好文:统信UOS桌面操作系统上使用ventoy制作U盘启动盘hello,大家好啊,今天给大家带来一篇关于vmware虚拟机上实现host与guest文件及目录互相复制粘贴的文章。本次示例用到的虚拟化是vmwareworkstationpro17,host用的是windows11,guest用的是统信UOS桌面操作系统1060。本次的目标......
  • Java 中的比较 equals 和 ==
    这个问题在Java面试的时候大概率会被问到。不是因为这个问题有什么复杂的,只是因为这个地方超出人类认知,你相信吗?比较什么如果你上培训班或者在学校学习的话,你的老师大概率可能会告诉你对于:基本类型:比较的是值是否相同;引用类型:比较的是引用是否相同;本来这里就有点复杂了,还非要搞出......
  • uniapp微信小程序对话框取消确认按钮
    uniapp微信小程序对话框实现,样式用scss写的,标题+内容+按钮效果图data(){ return{ refundMask:false,}}<viewclass="refund-button"><buttonclass="refund-button-btnplain"@click="handleRefund()">申请退款</button></vi......
  • 说完 Java 的 Abstract 后再来说说接口 (interface )
    如你对Abstract修饰的抽象类不是非常了解的话,请自行先考古下。这篇文章需要对Java定义过的抽象类有一些基本的了解才可以。抽象类和抽象方法用Abstract修饰的类,叫做抽象类,那么用Abstract修饰的方法叫做抽象方法。在Java中,喜欢用一些修饰关键字来对类或者变量或者方法来进......
  • 系统编程 文件描述符重定向
    open打开一个文件,返回的是该文件文件描述符程序中用文件描述符表管理文件描述符 默认1024个【0-1023】0 1 2 被系统占用 0是标准输入,1是标准输出,2是标准错误#include<stdio.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl......
  • Sentinel 工作原理详解
    1综述Sentinel所有资源都对应:一个资源名称一个EntryEntry可通过:对主流框架适配自动创建也可通过注解的方式或调API显式创建每个Entry创建同时也会创建一系列功能插槽(slotchain)。这些插槽有不同职责如:NodeSelectorSlot负责收集资源的路径,并将这些资源的......