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'这样就可以拿到子组件的数据并展示出来

相关推荐
大鱼七成饱15 分钟前
彻底搞懂 react 的 use client
前端·javascript·next.js
CUIYD_198932 分钟前
Ajax 核心知识点全面总结
前端·ajax·okhttp
菜鸡上道1 小时前
HTTP 请求中的 `Content-Type` 类型详解及前后端示例(Vue + Spring Boot)
vue.js·spring boot·http
Eliauk__1 小时前
深入浅出聊聊跨域:它到底是个啥,怎么破?
前端·javascript·面试
行星飞行1 小时前
Review-Gate MCP,让你的 cursor request 次数翻 5 倍
前端
小磊哥er1 小时前
【前端AI实践】DeepSeek:开源大模型的使用让开发过程不再抓头发
前端·vue.js·ai编程
Vesper631 小时前
【Vue】Vue2/3全局属性配置全攻略
前端·javascript·vue.js
伍哥的传说1 小时前
React Toast组件Sonner使用详解、倒计时扩展
前端·javascript·react.js·前端框架·ecmascript
徐志伟啊1 小时前
ElTree组件可以带线了?
前端·vue.js·前端框架
&白帝&1 小时前
Vue 3 常用响应式数据类型详解:ref、reactive、toRef 和 toRefs
前端·javascript·vue.js