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

网站vip怎么做互联网营销师报名费

网站vip怎么做,互联网营销师报名费,厦门网站建设哪家好,设计师的免费设计软件组件通信 一.$attrs(祖>孙间接)二、$refs()父>子, $parent()子>父三.provide,inject(祖>孙直接)四.pinia五.slot1.默认插槽2.具名插槽3.作用域插槽 一.$attrs(祖>孙间接) $attrs用于实现当前组件的父组…

组件通信

  • 一.$attrs(祖=>孙间接)
  • 二、$refs()父=>子, $parent()子=>父
  • 三.provide,inject(祖=>孙直接)
  • 四.pinia
  • 五.slot
    • 1.默认插槽
    • 2.具名插槽
    • 3.作用域插槽

一.$attrs(祖=>孙间接)

$attrs用于实现当前组件的父组件,向当前组件的子组件通信(祖→孙)。
$ attrs是一个对象,包含所有父组件传入的标签属性。
注意:$attrs会自动排除props中声明的属性(可以认为声明过的 props 被子组件自己“消费”了)
和props用法差不多
父组件

<template><div class="father"><h3>父组件</h3><Child :a="a" :b="b" :c="c" :d="d" v-bind="{x:100,y:200}" :updateA="updateA"/></div>
</template><script setup lang="ts" name="Father">import Child from './Child.vue'import { ref } from "vue";let a = ref(1)let b = ref(2)let c = ref(3)let d = ref(4)function updateA(value){a.value = value}
</script>

子组件:

<template><div class="child"><h3>子组件</h3><GrandChild v-bind="$attrs"/></div>
</template><script setup lang="ts" name="Child">import GrandChild from './GrandChild.vue'
</script>

孙组件

<template><div class="child"><h3>子组件</h3><GrandChild v-bind="$attrs"/></div>
</template><script setup lang="ts" name="Child">import GrandChild from './GrandChild.vue'
</script>

二、$refs()父=>子, $parent()子=>父

$ refs父亲拿取孩子的数据,$parent孩子拿取父亲的数据
1.原理
$refs:值为对象,包含所有被ref属性标识的DOM元素或组件实例。
$parent:值为对象,当前组件的父组件实例对象。
2.传递数据的需要把数据暴露出来才能被用
用defineExpose()暴露

// 宏函数把数据交给外部
defineExpose({ toy, book })

父组件

<template><div class="father"><h3>父组件</h3><h4>房产:{{ house }}</h4><button @click="changeToy">修改Child1的玩具</button><button @click="getAllChild($refs)">让所有孩子的书变多</button><Child1 ref="c1"/></div>
</template><script setup lang="ts" name="Father">import Child1 from './Child1.vue'import { ref,reactive } from "vue";let c1 = ref()// 数据let house = ref(4)// 方法function changeToy(){c1.value.toy = '小猪佩奇'}function getAllChild(refs:{[key:string]:any}){console.log(refs)for (let key in refs){refs[key].book += 3}}// 向外部提供数据defineExpose({house})
</script>

子组件

<template><div class="child1"><h3>子组件1</h3><h4>玩具:{{ toy }}</h4><h4>书籍:{{ book }}</h4><button @click="minusHouse($parent)">干掉父亲的一套房产</button></div>
</template><script setup lang="ts" name="Child1">import { ref } from "vue";// 数据let toy = ref('奥特曼')let book = ref(3)// 方法function minusHouse(parent:any){parent.house -= 1}// 把数据交给外部defineExpose({toy,book})</script>

三.provide,inject(祖=>孙直接)

具体使用:
在祖先组件中通过provide配置向后代组件提供数据
在后代组件中通过inject配置来声明接收数据
祖组件:

<template><div class="father"><h3>父组件</h3><h4>资产:{{ money }}</h4><h4>汽车:{{ car }}</h4><button @click="money += 1">资产+1</button><button @click="car.price += 1">汽车价格+1</button><Child/></div>
</template><script setup lang="ts" name="Father">import Child from './Child.vue'import { ref,reactive,provide } from "vue";// 数据let money = ref(100)let car = reactive({brand:'奔驰',price:100})// 用于更新money的方法function updateMoney(value:number){money.value += value}// 提供数据provide('moneyContext',{money,updateMoney})provide('car',car)
</script>

孙组件:

<template><div class="grand-child"><h3>我是孙组件</h3><h4>资产:{{ money }}</h4><h4>汽车:{{ car }}</h4><button @click="updateMoney(6)">点我</button></div>
</template><script setup lang="ts" name="GrandChild">import { inject } from 'vue';// 注入数据let {money,updateMoney} = inject('moneyContext',{money:0,updateMoney:(x:number)=>{}})let car = inject('car')
</script>

四.pinia

参考之前的笔记

五.slot

1.默认插槽

父组件中:<Category title="今日热门游戏"><ul><li v-for="g in games" :key="g.id">{{ g.name }}</li></ul></Category>
子组件中:<template><div class="item"><h3>{{ title }}</h3><!-- 默认插槽 --><slot></slot></div></template>

2.具名插槽

父组件中:<Category title="今日热门游戏"><template v-slot:s1><ul><li v-for="g in games" :key="g.id">{{ g.name }}</li></ul></template><template #s2><a href="">更多</a></template></Category>
子组件中:<template><div class="item"><h3>{{ title }}</h3><slot name="s1"></slot><slot name="s2"></slot></div></template>

3.作用域插槽

数据在组件的自身(子组件),但根据数据生成的结构需要组件的使用者(父组件)来决定。(新闻数据在News组件中,但使用数据所遍历出来的结构由App组件决定)

父组件中:<Game v-slot="params"><!-- <Game v-slot:default="params"> --><!-- <Game #default="params"> --><ul><li v-for="g in params.games" :key="g.id">{{ g.name }}</li></ul></Game>子组件中:<template><div class="category"><h2>今日游戏榜单</h2><slot :games="games" a="哈哈"></slot></div></template><script setup lang="ts" name="Category">import {reactive} from 'vue'let games = reactive([{id:'asgdytsa01',name:'英雄联盟'},{id:'asgdytsa02',name:'王者荣耀'},{id:'asgdytsa03',name:'红色警戒'},{id:'asgdytsa04',name:'斗罗大陆'}])</script>
http://www.tj-hxxt.cn/news/11039.html

相关文章:

  • 南京做网站建设的公司排名西安的网络优化公司
  • 怎样建网站步骤sem论坛
  • 小型的游戏网站怎么做百度一下官方下载安装
  • 四川九江龙钢结构网架公司aso优化技术
  • 下瓦房做网站公司企业网站的基本功能
  • 什么公司可以做网站排名优化关键词公司
  • 博罗网站建设海外营销公司
  • 网站的网站地图怎么做关键词查网址
  • 触摸屏网站如何做兰州seo公司
  • 网站开发课程知识点总结宁波seo推广优化公司
  • wordpress怎么都是英文版百度seo营销
  • 门户网站设计技巧网站seo优化公司
  • 页面好看的网站seo关键词优化推广价格
  • 怎么查网站外链seo优化课程
  • 网站公告栏模板站长工具ip查询
  • 网站建设收益分析杭州网站优化效果
  • 贸易公司网站源码东莞疫情最新消息通知
  • 网站制作及实现什么是网络整合营销
  • 个人优秀网页设计图片济南seo优化外包服务
  • 想学学做网站搜狗收录提交
  • 青岛网站设计建议i青岛博采四川疫情最新情况
  • div css制作个人网站北京建站公司
  • 网站关键词库怎么做百度广告业务
  • 横峰县城乡建设网站苏州网站建设开发公司
  • 龙岩网站设计找哪家好短视频营销推广
  • WordPress搬家注意事项北京云无限优化
  • 坪山网站建设哪家好百度收录检测
  • 什么公司网站建设做的好seo研究院
  • 深圳傻瓜式网站建设公司好吗互联网推广平台
  • 从美洲开始做皇帝免费阅读网站今日头条新闻在线看