闭包的应用1(导航栏)

在这里插入图片描述
MenuCompontent.vue

<template>
  <div>
    <ul>
      <li v-for="(item, i) in menusArr" :key="i + 'aa'" @click.stop="toRouter(item)">
        {{ item.name }}
           <menu-compontent :menusArr="item.children" v-if="item.children">
        </menu-compontent>
      </li>
    </ul>
  </div>

</template>

<script>
import MenuCompontent from './MenuCompontent.vue'
export default {
   components:{
       MenuCompontent
  },
  name: "menu-compontent",
  props:['menusArr'],
  data() {
    return {};
  },
  methods:{
    toRouter(item) {
      console.log(item.path)

    },
  },
 
};
</script>

<style></style>

pag.vue

<template>
  <div>
    <menu-compontent :menusArr="menusArr"> </menu-compontent>

  </div>
</template>

<script>
import MenuCompontent from "./MenuCompontent.vue";
export default {
  components: {
    MenuCompontent
  },
  data() {
    return {
      //想要改造成的数据:
      menusArr: [
        {
          categoryId: 7,
          parent: 0,
          name: "test0",
          path:'/test0',
          children: [{ categoryId: 74, parent: 7, name: "test5",path:'/test5', }]
        },
        {
          categoryId: 8,
          parent: 0,
          name: "test1",
          path:'/test1',
          children: [
            { categoryId: 20, parent: 8, name: "test6",path:'/test6', },
            { categoryId: 21, parent: 8, name: "test7",path:'/test7', },
            { categoryId: 67, parent: 8, name: "test8",path:'/test8', }
          ]
        },
        {
          categoryId: 9,
          parent: 0,
          name: "test2",path:'/test2',
          children: [
            {
              categoryId: 185,
              parent: 9,
              name: "test9",
              path:'/test9',
              children: [
                { categoryId: 190, parent: 185, name: "test14",path:'/test14' },
                { categoryId: 191, parent: 185, name: "test15",path:'/test15' },
                { categoryId: 192, parent: 185, name: "test16",path:'/test16' }
              ]
            }
          ]
        },
        {
          categoryId: 68,
          parent: 0,
          name: "test3",
          path:'/test3',
          children: [
            { categoryId: 186, parent: 68, name: "test10",path:'/test10', },
            { categoryId: 187, parent: 68, name: "test11",path:'/test11', },
            { categoryId: 188, parent: 68, name: "test12",path:'/test12', },
            { categoryId: 189, parent: 68, name: "test13",path:'/test13', }
          ]
        },
        {
          categoryId: 349,
          parent: 0,
          name: "test4",
          path:'/test4',
          children: [
            { categoryId: 350, parent: 349, name: "test17",path:'/test17', },
            { categoryId: 351, parent: 349, name: "test18",path:'/test18', }
          ]
        }
      ]
    };
  },
  created() {

  },
  methods: {
  }
};
</script>

<style></style>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
闭包是一种函数的编程概念,它可以捕获并记住外部函数的状态信息,使得函数能够访问和操作外部函数的变量。在Python中,闭包常常被用于以下几个方面的应用: 1. 延迟计算:通过使用闭包,我们可以实现延迟计算,即将一些计算推迟到函数被调用的时候再执行。这在需要缓存结果或者进行惰性计算的情况下非常有用。 ```python def add_numbers(a, b): def add(): return a + b return add result = add_numbers(5, 10) print(result()) # 输出15 ``` 2. 数据封装:闭包可以将函数和一些相关的数据进行封装,形成一个整体,使得数据和操作数据的函数能够紧密地结合在一起,方便使用和维护。 ```python def counter(): count = 0 def increment(): nonlocal count count += 1 return count return increment counter1 = counter() print(counter1()) # 输出1 print(counter1()) # 输出2 counter2 = counter() print(counter2()) # 输出1 ``` 3. 实现装饰器:闭包可以用于实现装饰器,即在不修改被装饰函数源代码的情况下,给函数添加额外的功能。 ```python def logger(func): def wrapper(*args, **kwargs): print(f"Calling function: {func.__name__}") result = func(*args, **kwargs) print(f"Function {func.__name__} called") return result return wrapper @logger def add(a, b): return a + b print(add(5, 10)) # 输出15 ``` 这些只是闭包在Python中的一些常见应用,通过使用闭包,我们可以编写更加灵活和高效的代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值