首页 > 其他分享 >作用域插槽

作用域插槽

时间:2022-08-22 14:44:59浏览次数:63  
标签:control index 作用域 插槽 item currentIndex tab props

父组件 : 

<template>
  <div class="app">
    <tab-control :titles="['衣服', '鞋子', '裤子']" 
                 @tab-item-click="tabItemClick">
      <template v-slot="props">
        <button>{{ props.item }} - {{props.abc}} </button>
      </template>
    </tab-control>
  </div>
</template>

<script>
  import TabControl from './TabControl.vue'

  export default {
    components: {
      TabControl
    },
    data() {
      return {
        pageContents: [ "衣服列表", "鞋子列表", "裤子列表" ],
        currentIndex: 0
      }
    },
    methods: {
      tabItemClick(index) {
        console.log("app:", index)
        this.currentIndex = index
      }
    }
  }
</script>

<style scoped>
</style>
子组件 : 

<template>
  <div class="tab-control">
    <template v-for="(item, index) in titles" :key="item">
      <div class="tab-control-item"
           :class="{ active: index === currentIndex }"
           @click="itemClick(index)">
        <slot :item="item" abc="cba">
          <span>{{ item }}</span>
        </slot>
      </div>
    </template>
  </div>
</template>

<script>
  export default {
    props: {
      titles: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        currentIndex: 0
      }
    },
    emits: ["tabItemClick"],
    methods: {
      itemClick(index) {
        this.currentIndex = index
        this.$emit("tabItemClick", index)
      }
    }
  }
</script>

<style scoped>
  .tab-control {
    display: flex;
    height: 44px;
    line-height: 44px;
    text-align: center;
  }

  .tab-control-item {
    flex: 1;
  }

  .tab-control-item.active {
    color: red;
    font-weight: 700;
  }

  .tab-control-item.active span {
    border-bottom: 3px solid red;
    padding: 8px;
  }
</style>

 

 

标签:control,index,作用域,插槽,item,currentIndex,tab,props
From: https://www.cnblogs.com/qd-lbxx/p/16612748.html

相关文章

  • 普通插槽
    父组件:<template><divclass="app"><!--1.内容是button--><show-messagetitle="哈哈哈"><button>我是按钮元素</button></show-message......
  • 具名插槽
    父组件:<template><nav-bar><template#left><button>{{leftText}}</button></template><template#center><span>内容</span>......
  • 日常开发记录-elementUI表格特殊值标红,利用插槽,vue动态绑定类名
    代码:<template><el-table:data="tableData"style="width:100%"><el-table-columnprop="date"label="日期"width="150"></......
  • 作用域
    变量提升在代码执行之前,会先进行代码的预解析,将var和function声明的变量进行提升,提升为window的属性(全局变量),并将var声明的变量赋值为undefined,var的提升在function之前......
  • JS 作用域和作用域链
    js的作用域是让我们访问变量和函数的区域,作用域规定了如何查找变量;js有两种作用域:全局作用域,局部作用域;局部作用域又可分为函数作用域,块级作用域和其它具体的作用域......
  • js-数据类型-作用域-作用域链-变量 审核中
    变量变量声明的几种方式varletconst相同之处varletconst都可以用来声明一个变量都拥有函数作用域与全局作用域不同之处var声明的变量有变量提升而let......
  • ASP.NET Core :容器注入(二):生命周期作用域与对象释放
    //瞬时生命周期ServiceCollectionservices=newServiceCollection();services.AddTransient<TestServiceImpl>();using(ServiceProvidersp=services.BuildServic......
  • 13、名称空间与作用域
    13、名称空间与作用域 目录:一名称空间1.1内建名称空间1.2全局名称空间1.3局部名称空间二作用域2.1全局作用域与局部作用域2.2作用域与名......
  • VUE学习-插槽
    插槽匿名插槽子组件设置匿名插槽<scripttype="text/x-template"id="custom-comp"><divclass="custom-comp-container"><h2>{{title}}</h2>......
  • Java变量与常量、作用域
    变量与常量、作用域1、变量(variable)变量:可以变化的量!Java是一种强类型语言,每个变量都必须声明其类型。Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作......