vue中面包屑的动态数据步骤

第一步

在路由文件index.js添加meta

{
        path: '/index',
        name: 'index',
        meta: { title: '首页' },
        component: () => import( '../views/home/index/index.vue')
      }

第二步

获取数据

1.return里

return {
      lists: [],
    };

2.获取路由数据信息

 mounted() {
    this.lists = this.$route.matched;
  },

3.在el-breadcrumb-item添加v-for="item in lists" :key="item.path"

 <el-breadcrumb-item v-for="item in lists" :key="item.path">{{item.meta.title}}</el-breadcrumb-item>

4.添加监听watch

 watch: {
    $route(to, from) {
      this.lists = this.$route.matched;
    },
  },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 设置面包屑通常需要以下步骤: 1. 在路由设置定义每个路由的名称和路径。 2. 在需要显示面包屑的组件,使用 $route 对象获取当前路由信息。 3. 根据当前路由信息,生成面包屑导航的数据结构。 4. 在模板使用 v-for 循环渲染面包屑导航。 以下是一个基本的面包屑实现示例: 1. 在路由设置定义每个路由的名称和路径。 ```js const routes = [ { path: '/', name: 'Home', component: Home }, { path: '/about', name: 'About', component: About }, { path: '/products', name: 'Products', component: Products, children: [ { path: 'laptop', name: 'Laptop', component: Laptop }, { path: 'phone', name: 'Phone', component: Phone } ] } ] ``` 2. 在需要显示面包屑的组件,使用 $route 对象获取当前路由信息。 ```js <script> export default { name: 'ProductDetail', data() { return { product: {} } }, mounted() { // 通过 $route.params 获取路由参数 this.product = this.$route.params.product } } </script> ``` 3. 根据当前路由信息,生成面包屑导航的数据结构。 ```js <script> export default { name: 'ProductDetail', computed: { // 生成面包屑导航数据结构 breadcrumbs() { let breadcrumbs = [ { text: 'Home', to: '/' }, { text: 'Products', to: '/products' }, { text: this.product.name, to: '' } ] return breadcrumbs } }, data() { return { product: {} } }, mounted() { // 通过 $route.params 获取路由参数 this.product = this.$route.params.product } } </script> ``` 4. 在模板使用 v-for 循环渲染面包屑导航。 ```html <template> <div> <nav> <ol class="breadcrumb"> <li v-for="(breadcrumb, index) in breadcrumbs" :key="index" class="breadcrumb-item"> <router-link :to="breadcrumb.to">{{ breadcrumb.text }}</router-link> </li> </ol> </nav> <h2>{{ product.name }}</h2> <p>{{ product.description }}</p> </div> </template> ``` 在以上示例面包屑导航的数据结构是一个数组,包含每个面包屑项的文本和链接,使用 v-for 循环渲染到模板。 注意:以上示例仅供参考,具体实现方式可能因项目需求而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值