vue slot槽

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  </head>

  <body>
    <div id="app">
      <comp> </comp>

      <!-- 槽替换 -->
      <comp>
        <p>{{message}}</p>
      </comp>

      <comp>
        <i>i标签</i>
      </comp>

      <comp>
        <button type="button">按钮</button>
      </comp>
    </div>

    <template id="comp">
      <div id="">
        <p>{{message}}</p>
        <slot>
          <!-- 默认槽 -->
          <button type="button">点我</button>
        </slot>
      </div>
    </template>

    <script>
      // 子组件
      const comp = Vue.extend({
        template: '#comp',
        data() {
          return {
            message: 'Hello furong!',
          }
        },
      })

      // root
      const app = new Vue({
        el: '#app',
        data: {
          message: 'Hello Vue.js!',
        },
        components: {
          comp,
        },
      })
    </script>
  </body>
</html>

posted @ 2021-07-06 14:12  thomas_blog  阅读(22)  评论(0编辑  收藏  举报