vue知识点————插槽 slot

slot 插槽 在父组件中引用的子组件 在父组件中写入百度 可在子组件slot插槽中展示出

父组件

html 复制代码
<template>
  <div id="app">
    <child url="https://www.baidu.com">百度</child>
  </div>
</template>

<script>
import child from "./components/child.vue";

export default {
  name: "App",
  components: {
    child,
  },
};
</script>

<style>
</style>

子组件

html 复制代码
<template>
  <div class="child">
    <a :href="url">
      <slot></slot>
    </a>
  </div>
</template>

<script>
export default {
  name: "nav_child",
  props: ["url"],
};
</script>

插槽的作用域

父组件

html 复制代码
<template>
  <div id="app">
    <child url="https://www.baidu.com">百度---{{ user.name }}</child>
  </div>
</template>

<script>
import child from "./components/child.vue";

export default {
  name: "App",
  components: {
    child,
  },
  data() {
    return {
      user: {
        name: "作用域",
      },
    };
  },
};
</script>

<style>
</style>

子组件

html 复制代码
<template>
  <div class="child">
    <a :href="url">
      <slot></slot>
    </a>
  </div>
</template>

<script>
export default {
  name: "nav_child",
  props: ["url"],
};
</script>


这里父组件可以访问父组件的数据并且可在slot中展示到,不可以在父组件中访问子组件的数据,就相当于父组件百度---{{ user.name }}{{ url }}
这里的url是访问不到的 会报错,想拿到子组件的数据请看下面的代码

具名插槽

父组件

html 复制代码
<template>
  <div id="app">
    <child>
      <template v-slot:header>
        <h1>header是标题</h1>
      </div>
      <template>
        <p>这是一段内容在匿名插槽中显示</p>
      </template>
      <template v-slot:footer>
        <p>footer是底部</p>
      </template>
    </child>
  </div>
</template>

<script>
import child from "./components/child.vue";

export default {
  name: "App",
  components: {
    child,
  },
  data() {
    return {
      
    };
  },
};
</script>

<style>
</style>

子组件

html 复制代码
<template>
  <div class="child">
    <header>
      <slot name="header"></slot>
    </header>
    <main>
      <!-- 匿名 -->
      <slot></slot>
    </main>
    <footer>
      <slot name="footer"></slot>
    </footer>
  </div>
</template>

<script>
export default {
  name: "nav_child",
};
</script>

插槽中指定name 拿到对应的数据展示,如果没有知道成为匿名也就拿没有定义name的数据

插槽父组件访问子组件数据

父组件

html 复制代码
<template>
  <div id="app">
    <child url="https://www.baidu.com" v-slot="slotProp"
      >百度---{{ user.name }}地址为{{ slotProp.url }}</child
    >
  </div>
</template>

<script>
import child from "./components/child.vue";

export default {
  name: "App",
  components: {
    child,
  },
  data() {
    return {
      user: {
        name: "作用域",
      },
    };
  },
};
</script>

<style>
</style>

子组件

html 复制代码
<template>
  <div class="child">
    <a :href="url">
      <slot :url="url"></slot>
    </a>
  </div>
</template>

<script>
export default {
  name: "nav_child",
  props: ["url"],
};
</script>

通过子组件在slot 上传递url值 在父组件中利用v-slot='slotProp'这样就可以拿到子组件的数据并展示出来

相关推荐
南半球与北海道#12 分钟前
前端引入vue-super-flow流程图插件
前端·vue.js·流程图
然我19 分钟前
React 16.8:不止 Hooks 那么简单,这才是真正的划时代更新 🚀
前端·react.js·前端框架
小高00732 分钟前
📈前端图片压缩实战:体积直降 80%,LCP 提升 2 倍
前端·javascript·面试
OEC小胖胖35 分钟前
【React Hooks】封装的艺术:如何编写高质量的 React 自-定义 Hooks
前端·react.js·前端框架·web
BillKu43 分钟前
vue3+element-plus 输入框el-input设置背景颜色和字体颜色,样式效果等同于不可编辑的效果
前端·javascript·vue.js
每天学习一丢丢1 小时前
Spring Boot + Vue 项目用宝塔面板部署指南
vue.js·spring boot·后端
惊悚的毛毛虫1 小时前
掘金免广告?不想看理财交流圈?不想看exp+8?
前端
springfe01011 小时前
vue3组件 - 大文件上传
前端·vue.js
再学一点就睡1 小时前
Vite 工作原理(简易版)—— 从代码看核心逻辑
前端·vite
好好好明天会更好1 小时前
uniapp项目中小程序的生命周期
前端·vue.js