v-for="(item, index) in $route.matched"有什么用
时间: 2024-04-05 10:29:14 浏览: 130
在Vue.js中,`v-for`是一个用于渲染列表数据的指令。在你提供的代码中,`v-for="(item, index) in $route.matched"`会遍历 `$route.matched` 数组中的路由对象,并将每个对象存储在 `item` 变量中,将当前对象的索引存储在 `index` 变量中。这允许你在模板中动态地渲染面包屑导航的每个面包屑项。
相关问题
v-for="(item, index) in $route.matched" 中的.matched属性有什么用
$route是Vue.js中的路由对象,它包含了当前导航的相关信息。其中,matched属性是一个数组,包含了当前路由的所有嵌套路径片段的路由记录。每个路由记录是一个对象,包含了路径、参数、查询参数等信息。在v-for="(item, index) in $route.matched"中,我们可以遍历matched数组中的每个路由记录,获取每个路由记录的信息,例如路径、参数、查询参数等,以便在路由导航时进行相关的操作。
<template> <div> <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb"> <el-breadcrumb-item><a href="/">首页</a></el-breadcrumb-item> <el-breadcrumb-item v-for="bread in breadList" v-bind:key="bread"> {{ breadsName[bread] }} </el-breadcrumb-item> </el-breadcrumb> </div> </template> <script lang="ts"> import { Provide, Component, Watch, Prop, Vue } from 'vue-property-decorator'; @Component export default class BreadCrumb extends Vue { @Provide() private breadsName: { [index: string]: string } = { '1': 'title1', '1-1': 'title1-1', '1-2': 'title1-2', '1-3': 'title1-3', }; @Provide() private breadList: string[] = []; private created() { this.getBreadList(); } @Watch('$route') private getBreadList() { this.breadList = []; for (const routeInfo of this.$route.matched) { if (typeof routeInfo.name === 'string' && routeInfo.name !== 'home') { this.breadList.push(routeInfo.name); } } } } </script> <style lang="less"> .breadcrumb { margin-bottom: 20px; } </style>
这是一个 Vue.js 组件,用于生成面包屑导航。组件使用了 ElementUI 的 el-breadcrumb 和 el-breadcrumb-item 组件,并且对 el-breadcrumb 进行了一些样式设置。组件的核心逻辑是通过 $route 对象获取当前页面的路由信息,然后根据路由信息生成对应的面包屑导航项。breadsName 对象用于存储路由信息对应的导航项名称,breadList 数组用于存储当前页面的所有导航项的路由信息。组件通过监听 $route 对象的变化实时更新导航项信息。
阅读全文
相关推荐














