Vue插槽

插槽

利用<slot>标签可以实现  组件 在不同的父组件中 使用 有不同的 html 结构

<!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="../../vue.js"></script>
</head>
<body>
  <div id="app">
    <home></home>
  </div>
  <template id="tql">
    <child>
      <i>你好</i>
    </child>
  </template>
</body>
<script>
  const Child = {
    template: `
      <div>
        <slot /> 
        <h1>我是child主体内容</h1>
        以下地方在 不同组件中使用 内容不一样
        <hr width="300"> 
        <!--
          slot 是一个占位符
          最终 会被 组件自定义标签内容所填充,内容是什么 slot就是什么
        -->
      </div>
    `
  }
  const Home = {
    template: "#tql",
    components: {
      Child
    }
  }
  Vue.component("home",Home);
  const vm = new Vue({
    el: "#app",
    data: {

    }
  })
</script>
</html>

具名插槽 (插槽起了名字  有多个插槽)

允许我们定义多个插槽,每个插槽起一个名字,在 嵌套内容可以根据名字 一一对应,从而实现在组件中不同的位置实现自定义的Html样式。

<!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="../../vue.js"></script>
</head>
<body>
  <div id="app">
    <home></home>
  </div>
  <template id="tql">
    <child>
      <i slot="s1">你好</i>
      <b slot="s2">Vue.js</b>
    </child>
  </template>
</body>
<script>
  const Child = {
    template: `
      <div>
        <slot name="s1"/> 
        <h1>我是child主体内容</h1>
        以下地方在 不同组件中使用 内容不一样
        <hr width="300"> 
        <slot name="s2"/>
        <!--
          slot 是一个占位符
          最终 会被 组件自定义标签内容所填充,内容是什么 slot就是什么
        -->
      </div>
    `
  }
  const Home = {
    template: "#tql",
    components: {
      Child
    }
  }
  Vue.component("home",Home);
  const vm = new Vue({
    el: "#app",
    data: {

    }
  })
</script>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值