内容分发与自定义事件(插槽)

2020-06-05

简单的插槽

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>myvue-001</title>
  </head>
  <body>
    <div id="app">
      <todo>
        <!-- 对slot进行绑定,勾搭上自定义组件中对slot -->
        <todo-title slot = "todo-title" v-bind:title="todoTitle"></todo-title>
        <todo-items slot="todo-items" v-for="item in todoItems" v-bind:item="item"></todo-items>
      </todo>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
    <script type="text/javascript">
    // 自定义总组建todo
      Vue.component("todo",{
        // slot相当于插槽,占位符,name为起一个名字
        template: "<div> \
                      <slot name='todo-title'></slot>  \
                      <ul>\
                        <slot name='todo-items'></slot>\
                      </ul>\
                   </div>"
      });
      // 自定义子组建todo-title
      Vue.component("todo-title",{
        // 传参,接收某个属性对值
        props: ['title'],
        // 定义模版
        template:"<div>{{title}}</div>"
      });
      // 自定义子组建todo-items
      Vue.component("todo-items",{
        // 传参,接收某个属性对值
        props: ['item'],
        // 定义模版
        template:"<li>{{item}}</li>"
      });
      var misterzhang = new Vue({
        el:'#app',
        data: {
          todoTitle: "标题",
          todoItems: ["张三","李四","王五"]
        }
      });
    </script>
  </body>
</html>

如果需要表格的话,需要自定义事件

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>myvue-001</title>
  </head>
  <body>
    <div id="app">
      <todo>
        <!-- 对slot进行绑定,勾搭上自定义组件中对slot -->
        <todo-title slot = "todo-title" v-bind:title="todoTitle"></todo-title>
        <!-- v-on:remove="removeItem" remove为emit中的名字,removeItem为Vue中的方法名 -->
        <todo-items slot="todo-items" v-for="(item,index) in todoItems" v-bind:item="item" v-bind:index="index" v-on:remove="removeItem(index)"></todo-items>
      </todo>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
    <script type="text/javascript">
    // 自定义总组建todo
      Vue.component("todo",{
        // slot相当于插槽,占位符,name为起一个名字
        template: "<div> \
                      <slot name='todo-title'></slot>  \
                      <ul>\
                        <slot name='todo-items'></slot>\
                      </ul>\
                   </div>"
      });
      // 自定义子组建todo-title
      Vue.component("todo-title",{
        // 传参,接收某个属性对值
        props: ['title'],
        // 定义模版
        template:"<div>{{title}}</div>"
      });
      // 自定义子组建todo-items
      Vue.component("todo-items",{
        // 传参,接收某个属性对值
        props: ['item','index'],
        // 定义模版
        template:"<li>{{index}}-{{item}} <button @click='remove(index);'>删除</button></li>",
        methods: {
         remove: function(index){
           //emit帮我们去触发vue中的方法,
           this.$emit("remove",index)
         } 
        }
      });
      var misterzhang = new Vue({
        el:'#app',
        data: {
          todoTitle: "标题",
          todoItems: ["张三","李四","王五"]
        },
        methods: {
          removeItem: function(index){
            // 删除
            this.todoItems.splice(index,1);
          }
        }
      });
    </script>
  </body>
</html>

标题:内容分发与自定义事件(插槽)
作者:张范
地址:http://misterzhang.top/articles/2020/06/05/1591349711651.html