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

合肥商务科技学校网站建设seo网站诊断顾问

合肥商务科技学校网站建设,seo网站诊断顾问,wordpress响应时间,海外营销平台有哪些前言:如何实现组件的灵活使用,今天学习组件封装用到的props、slot和emit。 目录 props 子组件 父组件 示例代码 slot 示例代码 作用域插槽 emit 示例代码 props 需要实现在其他组件中使用同一个子组件。 子组件 子组件(所谓子组件…

前言:如何实现组件的灵活使用,今天学习组件封装用到的props、slot和emit。

目录

props

子组件

父组件

示例代码

slot

示例代码

作用域插槽

emit

示例代码


props

需要实现在其他组件中使用同一个子组件。

子组件

子组件(所谓子组件,就是封装好的组件,供其他组件使用)

子组件定义了sonName

<div>我是MRJJ_9{{sonName}}</div>

defineProps(['sonName'])

或 const props=defineProps(['sonName'])

props是只读的,尽量不要去修改

定义多个

const props=defineProps(['sonName1','sonName2'])

但通常使用数组定义

const props = defineProps({
  sonName1: Object,
  sonName: Number,})

父组件

父组件(所谓父组件,就是引用封装好的其他子组件)

<Mrjj-Counter :sonName="sonName"></Mrjj-Counter>

let sonName=ref("引用子组件")

示例代码

子组件设置

<template><div>我是MRJJ_9的第一个属性,类型为字符串,内容是:{{ sonName1 }},第二个属性,类型是数字,数值为:{{ sonName2 }}</div>
</template>
<script setup>
const props = defineProps({sonName1: String,sonName2: Number,
})
console.log("属性1",props.sonName1)
console.log("属性2",props.sonName2)
</script>
<style lang="scss" scoped>
</style>

父组件设置

<template><mrjj-son :sonName1="sonName1" :sonName2="sonName2"></mrjj-son>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import { ref } from 'vue'
let sonName1 = ref('hello,world!!!')
let sonName2 = ref(999)
</script>
<style lang="scss" scoped>
</style>

 

要注意不能去修改里面的值

slot

需要实现在其他组件中使用同一个组件(子组件),但组件样式的有所区别

这就需要用到插槽:slot,其作用是传参时可以带上HTML结构

子组件带上slot

{{ sonName }}<slot></slot>

父组件将需要传递的内容写到子组件标签里

<mrjj-son><strong>{{sonName }}</strong></mrjj-son>

具名插槽,给插槽命名

有多个值时

子组件加上name

父组件,用v-slot:插槽名(或#插槽名)

示例代码

子组件设置

<template><div>{{ sonName1 }}<slot name="mrjj1"></slot>{{ sonName2 }}<slot name="mrjj2"></slot></div>
</template>
<script setup>
const props = defineProps({sonName1: String,sonName2: Number
})
</script>
<style lang="scss" scoped>
</style>

父组件设置

<template><mrjj-son><template #mrjj1><strong>{{ sonName1 }}</strong></template><template #mrjj2><i>{{ sonName2 }}</i></template></mrjj-son>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import { ref } from 'vue'
let sonName1 = ref('hello,world!!!')
let sonName2 = ref(999)
</script>
<style lang="scss" scoped>
</style>

效果展示

作用域插槽

子组件

<template><div>{{ sonName3 }}<slot name="mrjj3" :times="count" :mrjj1="name"></slot></div>
</template>
<script setup>
import { ref } from 'vue'const props = defineProps({sonName3: String
})
let count = ref(0)
let name = ref('计数器')
</script>
<style lang="scss" scoped>
</style>

父组件 

<template #mrjj3="{ times }"

<template><mrjj-son><template #mrjj3="{ times }"><i>{{ sonName3 }}</i><Times :times="times"></Times></template></mrjj-son>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import Times from '@/components/Times.vue'
import { ref } from 'vue'
let sonName3 = ref('')
</script>
<style lang="scss" scoped>
</style>

引用的Time.vue文件

<template><h1>显示Mrjj{{ times }}</h1>
</template>
<script setup>
defineProps(['times'])
</script>

emit

需求:增加一个关闭、打开的组件功能

用到emit,emit干了什么事情呢?在子组件中触发一个事件,在父组件中进行监听。

示例代码

子组件定义一个自定义事件

<template><div>{{ sonName1 }}<slot name="mrjj1"></slot>{{ sonName2 }}<slot name="mrjj2"></slot><button @click="closeSon">点我关闭</button></div>
</template><script setup>
const props = defineProps({sonName1: String,sonName2: Number
})
const emit = defineEmits(['closeMrjj'])function closeSon() {console.log('关闭按钮被点击了!')emit('closeMrjj')
}
</script>
<style lang="scss" scoped>
</style>

父组件绑定事件

<template><mrjj-son @closeMrjj="closeMrjj" v-if="isClose"><template #mrjj1><strong>{{ sonName1 }}</strong></template><template #mrjj2><i>{{ sonName2 }}</i></template></mrjj-son><button v-else @click="($event) => (isClose = true)">点我打开</button>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import { ref } from 'vue'
let sonName1 = ref('hello,world!!!')
let sonName2 = ref(999)
let isClose = ref(false)
function closeMrjj() {isClose.value = false
}
</script>
<style lang="scss" scoped>
</style>

效果展示

点我关闭按钮,点击后,调用了closeSon函数,可以看到console输出的信息。

点击展开后,也可以展示出内容。

http://www.yidumall.com/news/23992.html

相关文章:

  • 衢州网站设计排名中国十大门户网站排行
  • 公司网站定制推广平台免费b2b网站大全
  • 规范 加强网站建设管理百度seo排名查询
  • 建一个资源网站赚钱吗路由优化大师
  • 学电商设计大概多少钱南京seo推广公司
  • 清远企业网站建设点击器原理
  • 网站建设及优化 赣icp企业推广宣传文案
  • 深圳数字展厅京东关键词优化技巧
  • 做个网站要多少钱 一般媒体代发布
  • 厦门专业制作网站郑州网络推广代理
  • 企业网站怎做曲靖seo
  • 视频网站建设解决方案网站提交百度收录
  • 厦门律师网站建设搜索引擎的工作原理分为
  • 网站怎样做货到付款个人网页设计作品模板
  • 中国开头的网站怎么做百度关键词排名联系
  • 域名注册和网站哪个好app推广策划方案
  • 做网站一般不选用的图片格式郑州模板网站建设
  • 企业网站首页排版分析持续优化疫情防控举措
  • 做的不错的h5高端网站2023年东莞疫情最新消息
  • 福州金山网站建设新闻 今天
  • wordpress标签文章数量深圳关键词优化软件
  • 版式设计素材网站某个网站seo分析实例
  • 北京理工大学网站开发与应用鸿星尔克网络营销案例分析
  • 网站大气是什么意思网址注册查询
  • 基于目的地的o2o旅游电子商务网站开发设计毕业设计外贸网站推广方法之一
  • 网站简历文字如何空行网络营销活动策划
  • 做英文网站怎么赚钱南召seo快速排名价格
  • 横岗做网站百度产品大全首页
  • 中国做的很好的食品网站百度实时热搜榜
  • 免费空间做网站展示型网站有哪些