vue中使用插槽

有name属性的为具名插槽。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script  src="./node_modules/vue/dist/vue.js"></script>
</head>
<body>
  <div id="root">
    <body-content>
      <div>1111111无name</div>
      <div slot='header'>header</div>
      <div slot='footer'>footer</div>
      <div>2222222无name</div>

    </body-content>
  </div>
  <script>
    //slot添加name属性为具名插槽,显示对应name的内容,若无name属性,则显示全部无name属性的插槽
    Vue.component('body-content',{
      template:`<div>
                <slot>默认值</slot>
                <slot name='header'>默认值</slot>
                <div>body</div>
                <slot name='footer'>默认值</slot>
              </div>`
    })
    var vm=new Vue({
      el:'#root'
    })
  </script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/em2464/p/12329149.html