序言

组件的最大特性就是复用性,而用好插槽能大大提高组件的可复用能力

匿名插槽

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script type="text/javascript" src="../../assets/js/vue.js"></script>
</head>
<body>
  <div id="app">
    <!-- 父组件向子组件传递内容 -->
    <alert-box>有bug发生</alert-box>
    <alert-box>有一个警告</alert-box>
    <alert-box></alert-box>
  </div>

  <template id="slottmpl">
    <div>
      <strong>ERROR:</strong>
       <!-- 子组件插槽 -->
      <slot>默认内容</slot>
    </div>
  </template>

  <script type="text/javascript">
    // 子组件插槽
    Vue.component('alert-box', {
      template:'#slottmpl',
    });

    var vm = new Vue({
      el: '#app',
      data: {
        
      }
    });
  </script>
</body>
</html>
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-12
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-11-12
  • 2021-12-24
猜你喜欢
  • 2021-06-01
  • 2021-09-30
  • 2022-12-23
  • 2021-03-02
  • 2021-06-13
  • 2021-11-07
  • 2022-12-23
相关资源
相似解决方案