vue之template标签及插槽slot

template

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>template使用</title>
</head>
<!-- 
  在Vue中使用template标签的时候,此标签不会在页面中进行渲染显示
  当我们在做自定义组件的时候可以使用template标签在html代码中定义一个模本节点
    此节点放置在vue实例的作用范围之外,不会被渲染在页面上
    在定义自定义组件的时候可以根据template的id设置为自定义组件的模板
 -->
<body>
  <div id="app">
    <t2></t2>
    <div v-for="i in 5">
      <h5>我是h1</h5>
      <p>我是一段简介</p>
    </div>

    <template v-for="i in 5">
      <h5>我是h6</h5>
      <p>我是一段简介</p>
    </template>

    <template>
      <h2>我是h2</h2>
    </template>
    <t></t>
  </div>
  <template id="tpl">
    <div>
      <h2 style="color: deeppink;">我是h2</h2>
      <p>我是t组件中的一段话</p>
    </div>
  </template>
  <script src="./lib/vue.js"></script>
  <script>
    var app = new Vue({
      el: '#app',
      data: {
        list: [],
      },
      components: {
        t: {
          template: '#tpl'
        },
        t2: {
          template: `<template>
              <h3>我这是t2自定义组件</h3>
            </template>`
        }
      }
    })
  </script>
</body>
</html>

插槽

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>插槽</title>
</head>
<body>
  <!-- 
    插槽相当于一个占位符,当没有内容传递过去的时候会显示插槽的内容,有内容时不会显示插槽的内容

    具名插槽
      一个组件中可以使用多个插槽,为了区分每一个插槽的位置可以添加名字
        通过name属性设置名字
      在父组件中使用具名插槽的时候需要把插入的内容放置在template节点中
        使用v-slot:插槽的name值 进行绑定位置
        v-slot可以简写为#
      在既有具名插槽又有默认插槽的时候
        如果不使用v-slot进行绑定name会把所有的内容都插入在默认位置
   -->
  <div id="app">
    <child>
      <h5 style="color: deeppink;">我是一段话</h5>
    </child>
    <child>
      <h5 style="color: deeppink;">我是一段话</h5>
      <p style="color: deeppink; font-size: 0.8rem;">我是一段话</p>
      <p style="color: deeppink; font-size: 12.8px;">我是一段话</p>
    </child>
    <child2>
      <template v-slot:t>
        <h5 style="color: deeppink;">《三体》</h5>
      </template>
      <template #p>
        <p style="color: red;">¥39.28</p>
      </template>
      <p>不要回复,不要回复,不要回复......</p>
    </child2>
  </div>
  <script src="./lib/vue.js"></script>
  <script>
    var app = new Vue({
      el: '#app',
      components: {
        child: {
          template: `
          <div>
            <p>我是一行话</p>
            <slot>这是占位内容</slot>
          </div>
          `,
        },
        child2: {
          template: `
          <div>
            <h5>此处的内容展示多个插槽</h5>
            <div class="title">
              <slot name="t">此处应该显示标题内容</slot>
            </div>
            <div class="price">
              <slot name="p">此处显示价格</slot>
            </div>
            <div class="descriptions">
              <slot>此处应该显示简介内容</slot>
            </div>
          </div>
          `
        }
      }
    })
  </script>
</body>
</html>

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值