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

相关推荐
HIT_Weston9 分钟前
61、【Ubuntu】【Gitlab】拉出内网 Web 服务:Gitlab 配置审视(五)
前端·ubuntu·gitlab
剑小麟20 分钟前
vue2项目中安装vant报错的解决办法
vue.js·java-ee·vue
旺仔Sec31 分钟前
2026年度河北省职业院校技能竞赛“Web技术”(高职组)赛项竞赛任务
运维·服务器·前端
用户40993225021232 分钟前
Vue的Class绑定对象语法如何让动态类名切换变得直观高效?
前端·ai编程·trae
GIS之路1 小时前
GIS 数据转换:GDAL 实现将 CSV 转换为 Shp 数据(一)
前端
Nan_Shu_6141 小时前
学习:Vue (2)
javascript·vue.js·学习
武清伯MVP1 小时前
深入了解Canvas:HTML5时代的绘图利器(一)
前端·html5·canvas
一水鉴天1 小时前
整体设计 定稿 之24 dashboard.html 增加三层次动态记录体系仪表盘 之2 程序 (Q208 之1)
前端·html
北辰alk2 小时前
Vue项目Axios封装全攻略:从零到一打造优雅的HTTP请求层
vue.js
_杨瀚博2 小时前
微信支付集成_JSAPI
前端