当前位置: 首页 > news >正文

搭建网站架构怎么做创建公司网站难吗

搭建网站架构怎么做,创建公司网站难吗,龙岩建筑公司有哪些,wordpress添加ico目录 定义语法使用场景场景一场景二场景三tips只有一个默认插槽时 定义 在Vue中, v-slot 指令用于定义插槽的模板内容。它用于在父组件中传递内容到子组件中的插槽。 v-slot 指令可以用于 标签或组件标签上,以便在子组件中使用插槽。 语法 使用 v-slo…

目录

  • 定义
  • 语法
  • 使用场景
  • 场景一
  • 场景二
  • 场景三
  • tips
    • 只有一个默认插槽时

定义

在Vue中, v-slot 指令用于定义插槽的模板内容。它用于在父组件中传递内容到子组件中的插槽。 v-slot 指令可以用于 标签或组件标签上,以便在子组件中使用插槽。

语法

使用 v-slot 指令时,可以使用以下两种语法:

  1. 缩写语法: # 符号表示 v-slot 指令,后面跟插槽名称。
<template #插槽名称><!-- 插槽内容 -->
</template>
  1. 完整语法: v-slot 指令后面跟着 : ,后面是插槽名称。
<template v-slot:插槽名称><!-- 插槽内容 -->
</template>

使用场景

v-slot 指令的使用场景包括但不限于以下几种:

  • 在组件中使用插槽,将父组件中的内容传递给子组件。
  • 在子组件中使用具名插槽,根据插槽名称渲染不同的内容。
  • 在子组件中使用作用域插槽,将子组件中的数据传递到父组件中进行渲染。

场景一

在组件中使用插槽,将父组件中的内容传递给子组件。

父组件

<template><div><child-component><template v-slot:default><!-- 插槽内容 --><p>This is the content passed from the parent component.</p></template></child-component></div>
</template>

子组件

<template><div><slot></slot></div>
</template>

场景二

在子组件中使用具名插槽,根据插槽名称渲染不同的内容:

父组件

<template><div><child-component><template v-slot:header><!-- 插槽内容 --><h1>Header Content</h1></template><template v-slot:body><!-- 插槽内容 --><p>Body Content</p></template></child-component></div>
</template>

子组件

<template><div><slot name="header"></slot><slot name="body"></slot></div>
</template>

场景三

在子组件中使用作用域插槽,将子组件中的数据传递到父组件中进行渲染:

父组件

<template><div><child-component><template v-slot:default="slotProps"><!-- 插槽内容 --><p>{{ slotProps.message }}</p></template></child-component></div>
</template>

子组件

<template><div><slot :message="message"></slot></div>
</template><script>
export default {data() {return {message: "Hello from child component!"};}
};
</script>

在router-view中的应用,拿到router-view中的Component值,同时利用component 标签动态渲染组件

 <router-view v-slot="{ Component, route }"><transition appear name="fade-transform" mode="out-in"><keep-alive :include="keepAliveName"><component :is="Component" v-if="isRouterShow" :key="route.fullPath" /></keep-alive></transition></router-view>

tips

如果父组件没有向插槽传入值,则子组件会显示原来的内容,当传入具体的值时,则会覆盖掉插槽内的内容

子组件:

<template><slot name="a1" :content="slot_data"> <h1>child-123</h1> </slot>
</template><script lang="ts" setup>
const slot_data = "child-content";
</script><style scoped></style>

父组件:

<template><div><h5>slot-test</h5><child><!-- <template #a1="{ content }"><div>{{ content }}</div></template> --></child></div>
</template><script lang="ts" setup>
import child from "./child.vue";
</script><style scoped></style>

此时注释掉插值代码,结果如图,只会显示原来槽内内容
在这里插入图片描述

父组件代码修改如下

<template><div><h5>slot-test</h5><child><template #a1="{ content }"><div>{{ content }} 我是父组件</div></template></child></div>
</template><script lang="ts" setup>
import child from "./child.vue";
</script><style scoped></style>

显示内容如图所示,则会覆盖掉原来槽值
在这里插入图片描述

在v-slot中,既可以由子组件向父组件传值(slot_data),又可以由父组件向子组件传递html内容,可以看做是‘’双向的‘’
在一些场景比如子组件渲染的内容既需要子组件数据又需要父组件数据时可以考虑使用插槽来完成

props同样也可以向子组件传值,在子组件中同一渲染完成,这是之前一直使用的方式,之后可以考虑使用插槽,拿到子组件中的值,又可以向子组件传递内容

只有一个默认插槽时

可以直接这样写,类似于上述router-view的用法
子组件:

<template><slot :content="slot_data" :content2="slot_data2"> </slot>
</template><script lang="ts" setup>
const slot_data = "child-content";
const slot_data2 = "child-content2";
</script>

父组件:
content,content2采用解构赋值直接从slotProps值(默认传递变量的名称)中得到,templete也可以省略,child标签内的所有值都会被传入插槽

<template><div><h5>slot-test</h5><child v-slot="{ content, content2 }"><!-- <h1>{{ content }}</h1> -->{{ content }}{{ content2 }}784561</child></div>
</template><script lang="ts" setup>
import child from "./child.vue";
</script>

结果如图:
在这里插入图片描述

http://www.yayakq.cn/news/322327/

相关文章:

  • 陇南做网站网站推广优化方案模板
  • 什么网站做视频给钱顺德建设工程交易中心网站
  • 青岛建设银行银行招聘网站软件开发外包公司
  • 网站建设助君网络网站建设维护面试
  • 广州外贸营销网站建设公司北京海淀的公司
  • 专门找人做软件的网站长垣高端建站
  • seo是做网站个人申请小程序收费吗
  • 网页制作与网站建设技术大全民和县wap网站建设公司
  • 如何备份网站joomla3.8与wordpress
  • 学校网站建设开发商vue快速搭建网站
  • 网站 后台 数据 下载如何做英文网站推广
  • 在线设计网站排名设计公司网站 唐山
  • 给一个公司做网站需要多久wordpress 升级数据库
  • 酒店网站程序广州网站优化网站建设
  • 吴中seo网站优化软件厦门医院网站建设
  • 深圳市长城建设有限公司网站长春seo优化企业网络跃升
  • 网站建设栏目管理wordpress文章排序id
  • 网站建设自助建站云建站网站开发的疑虑
  • 怎样做旅游摄影网站联系深圳网站制作公司
  • 产品网站建设设计方案做ppt常用图片网站有哪些
  • wordpress做的网站电子商务网站建设评估工具有哪些
  • 网站备案可以更改吗2023年防疫新政策
  • 信息产业部icp备案中心网站wordpress批量删除图片
  • 做网站需要什么配置的电脑营销号经典废话
  • 蒙古文政务网站建设工作汇报自动化优化系统网站建设
  • seo站内站怎么做以网络营销为主题的论文
  • 一个域名解析多个网站洞头网站建设
  • 个人网店和网站的区别深圳网络公司推广平台
  • 石家庄网站建设加q.479185700自己做网站买东西
  • 聊城网站建设lchckj线上销售方案