码迷,mamicode.com
首页 > 其他好文 > 详细

Vue作用域插槽:用作循环结构的模版

时间:2019-08-03 12:42:45      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:效果   slot   port   return   作用域   for   ros   组件   type   

 

一 项目结构

 

技术图片

 

二 App组件

 

<template>
  <div id="app">
    <!-- 子组件 -->
    <todos :list="list"  v-slot:default="{item}">
      <!-- 插槽内容 -->
      <span v-if="item.isComplete">?</span>
      {{item.text}}
    </todos>
  </div>
</template>

<script>
import Todos from "./components/Todos.vue";

export default {
  name: "app",
  data() {
    return {
      list: [
        {
          isComplete: true,
          text: "联网"
        },
        {
          isComplete: false,
          text: "玩游戏"
        }
      ]
    };
  },
  components: {
    Todos
  }
};
</script>

<style>
</style>

 

三 Todos组件

 

<template>
    <ul>
        <li v-for="(item,index) in list" :key="index">
            <!-- 作用域插槽:用作循环结构的模版 -->
            <slot :item="item"/>
        </li>
    </ul>
</template>
<script>
export default {
  props: {
    list: {
      type: Array,
      default() {
        return [];
      }
    }
  }
};
</script>
<style>
</style>

 

四 运行效果

 

技术图片

 

Vue作用域插槽:用作循环结构的模版

标签:效果   slot   port   return   作用域   for   ros   组件   type   

原文地址:https://www.cnblogs.com/sea-breeze/p/11294230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!