学习-vue3 作用域插槽,帮助理解的简单demo

父组件:

<template>
  <div>
    <!-- 父级作用域中,可以使用 slot 定义我们提供的插槽 prop 的名字 -->
    <todo-list>
      <template v-slot:default="slotProps">
        <span class="green">{{ slotProps.item }}--</span>
        <span class="red">{{ slotProps.index }}</span>
      </template>
    </todo-list>
  </div>
</template>

<script>
import todoList from '../components/todo-list.vue'
export default {
  components: {
    todoList
  },
  data () {
    return {

    }
  }
}
</script>

<style lang="scss">
.green {
  color: green;
}
.red {
  color: red;
}
</style>

子组件:

<template>
  <div>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        <!-- item index 插槽 prop -->
        <slot :item="item" :index="index"></slot>
      </li>
    </ul>
  </div>
</template>

<script>

export default {
  data () {
    return {
      items: ['cat', 'dog', 'rabbit']
    }
  },
  methods: {

  }
}
</script>

<style>

</style>

 

posted on 2022-08-04 16:23  法老的微笑  阅读(89)  评论(0)    收藏  举报