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

网站前台乱码网站快速排名互点软件

网站前台乱码,网站快速排名互点软件,gwt 网站开发,成都培训学校网站建设Vue3父子组件传属性和方法调用Demo 说明目录父组件给子组件传值和方法 父组件给子组件传值-使用defineProps接受父组件属性值父组件给子组件传值-使用defineModel接受父组件v-model值 当子组件只需要接收父组件一个v-model值时,写法1如下:子组件接收单个v-model写法2如下:当子…
Vue3父子组件传属性和方法调用Demo
  • 说明
  • 目录
  • 父组件给子组件传值和方法
    • 父组件给子组件传值-使用defineProps接受父组件属性值
    • 父组件给子组件传值-使用defineModel接受父组件v-model值
      • 当子组件只需要接收父组件一个v-model值时,写法1如下:
      • 子组件接收单个v-model写法2如下:
      • 当子组件需要接收父组件多个v-model值时,写法如下:
    • 父组件给子组件传方法
  • 子组件调用父组件方法-使用defineEmits调用父组件方法
  • 子组件暴露属性和方法给父组件调用-使用defineExpose暴露子组件属性和方法

说明

这里记录下自己学习Vue3父子组件怎么传值和方法怎么互相调用,防止后面继续踩坑且方便以后直接使用。这里承接自己的博客Vue3+vite优化基础架构(3)— 优化vue-i18n国际化配置这篇博客,在该博客项目的基础上学习Vue3父子组件传值及使用。

官方文档:https://cn.vuejs.org/api/sfc-script-setup.html#defineprops-defineemits

目录

在这里插入图片描述
这里父组件是test-management1文件夹下的index.vue,子组件是test-management1文件夹下的components文件夹的test-child.vue。

父组件给子组件传值和方法

父组件给子组件传属性值,有2种方式:

  1. 第一种方式是在子组件里面使用defineProps来接受父组件那边传过来的属性值。
  2. 第二种方式是在子组件里面使用defineModel来接受父组件那边传过来的v-model值。

父组件给子组件传值-使用defineProps接受父组件属性值

父组件代码如下:

<template><div>测试管理1页面</div><div><!--在父组件里面使用子组件TestChild--><!--父组件向子组件传参,参数为name,age,isOpenEmail,content,score这5个--><TestChild:name="testName":age="testAge":isOpenEmail="testIsOpenEmail":content="testContent":score="testScore"/></div>
</template><script setup name="test-management1">import {ref} from "vue"//引入子组件import TestChild from './components/test-child.vue'//定义name值const testName=ref('张三')//定义age值const testAge=ref(18)//定义isOpenEmail开通邮箱值const testIsOpenEmail=ref(false)//定义content值const testContent=ref(['测试值1','测试值2','测试值3'])//定义score值const testScore=ref({curriculum:"语文",score:60})
</script><style scoped></style>

子组件代码如下:

<template><!--显示父组件传过来的参数值--><div><!--在页面模版里面如果要使用父组件里面穿过来的参数,有2种写法,都能在页面显示出来:--><!--第一种全写:{{props.name}}--><div>name名字为:{{props.name}}</div><!--第二种简写:{{name}},这种简写有个问题,如果你在js中定义了一个相同名的参数,它会优先读取定义的那个参数,不会去读父组件传过来的参数--><div>name名字为:{{name}}</div><div>age年龄为:{{props.age}}</div><div>isOpenEmail是否开通邮箱为:{{props.isOpenEmail}}</div><div>content内容为:{{props.content[0]}}</div><div>score分数为:{{props.score.score}}</div></div>
</template><script setup>import {onMounted} from 'vue'//const name="haha"//页面初始化时执行onMounted(() => {console.info("从父组件那边传过来的值为:")console.info("name:",props.name)console.info("age:",props.age)console.info("isOpenEmail:",props.isOpenEmail)console.info("content:",props.content)console.info("score:",props.score)})//使用defineProps接收父组件穿传过来的参数const props = defineProps({//从父组件那边接收一个String类型的参数,参数名叫name(名字)name: {required:false,//是否必须传该name参数,不写默认为false,true为必须传该参数,false为可以不传该name参数type: String,//参数类型为String字符串default: ''//默认值为空值},//从父组件那边接收一个Number类型的参数,参数名叫age(年龄)age: {type: Number,//参数类型为Number整型default: 0//默认值为0},//从父组件那边接收一个Boolean类型的参数,参数名叫isOpenEmail(是否开通邮箱)isOpenEmail: {type: Boolean,//参数类型为Boolean布尔类型default: false//默认值为false},//从父组件那边接收一个content类型的参数,参数名叫content(内容)content: {type: Array,//参数类型为Array数组类型default: []//默认值为空数组},//从父组件那边接收一个Object类型的参数,参数名叫score(分数)score: {type: Object,//参数类型为Object对象类型default: {}//默认值为空对象}})
</script><style scoped></style>

谷歌浏览器效果显示
在这里插入图片描述
注意事项
1.在页面模版中如果直接使用简写{{name}}这种写法,如果在js中又重新定义了一个name属性,那么会优先使用定义的这个name值,不会使用父组件那边传值过来的name属性值。如下:
在这里插入图片描述
在这里插入图片描述

2.如果子组件接收的参数有required属性而且为true,如果父组件不传该属性的话,那么控制台会出现警告。如下:
在这里插入图片描述
在这里插入图片描述
浏览器结果如下:
在这里插入图片描述

父组件给子组件传值-使用defineModel接受父组件v-model值

当子组件只需要接收父组件一个v-model值时,写法1如下:

父组件代码:

<template><div>测试管理1页面</div><div><!--在父组件里面使用子组件TestChild--><!--父组件向子组件传参,使用v-model形式进行双向绑定--><TestChildv-model="testName"/></div>
</template><script setup name="test-management1">import {ref} from "vue"//引入子组件import TestChild from './components/test-child.vue'//定义name值const testName=ref('张三')</script><style scoped></style>

子组件代码:

<template><!--显示父组件传过来的参数值--><div><el-input v-model="modelValue"></el-input></div>
</template><script setup>import {onMounted} from 'vue'//页面初始化时执行onMounted(() => {})//直接使用解构写法来接收父组件那边通过v-model传过来的值,必须使用modelValue来接收属性值const [modelValue]=defineModel()console.info("子组件接收父组件的name值为:",modelValue.value)
</script><style scoped></style>

谷歌浏览器结果如下:
在这里插入图片描述

子组件接收单个v-model写法2如下:
<template><!--显示父组件传过来的参数值--><div><el-input v-model="name"></el-input></div>
</template><script setup>import {onMounted} from 'vue'//页面初始化时执行onMounted(() => {console.info("子组件接收父组件的v-model值为:",name.value)})//直接使用defineModel接受父组件通过v-model传过来的值//子组件这边定义一个name属性来接收父组件那边传过来的v-model的值,简写形式如下const name=defineModel()//完整写法/*const name = defineModel({type: String,//字段类型default: ''//默认空值})*///等同于上面的default: ''写法/*const name = defineModel({type: String,//字段类型default: () => {//扩展:将默认属性作为一个方法使用,可以执行一段自定义逻辑,这里直接返回了一个空值,等同于上面写法return ''}})*/</script><style scoped></style>

浏览器结果如下:
在这里插入图片描述

当子组件需要接收父组件多个v-model值时,写法如下:

父组件代码:

<template><div>测试管理1页面</div><div><!--在父组件里面使用子组件TestChild--><!--父组件向子组件传参,使用v-model形式进行双向绑定--><!--绑定多个v-model时,v-model冒号后面为参数名称,后面为参数值--><!--向子组件传参属性为testName,testAge,testIsOpenEmail,testContent,testScore这5个参数--><TestChildv-model:testName="name"v-model:testAge="age"v-model:testIsOpenEmail="isOpenEmail"v-model:testContent="content"v-model:testScore="score"/></div>
</template><script setup name="test-management1">import {ref} from "vue"//引入子组件import TestChild from './components/test-child.vue'//定义name值const name=ref('张三')//定义age值const age=ref(18)//定义isOpenEmail开通邮箱值const isOpenEmail=ref(false)//定义content值const content=ref(['测试值1','测试值2','测试值3'])//定义score值const score=ref({curriculum:"语文",score:60})</script><style scoped></style>

子组件代码:

<template><!--显示父组件传过来的参数值--><div><!--使用父组件那边传过来的name值--><el-input v-model="name"></el-input></div>
</template><script setup>import {onMounted} from 'vue'//页面初始化时执行onMounted(() => {console.info("子组件接收父组件的v-model值为:")console.info("name:",name.value)console.info("age:",age.value)console.info("isOpenEmail:",isOpenEmail.value)console.info("content:",content.value)console.info("score:",score.value)})//直接使用defineModel接受父组件通过v-model传过来的值//子组件这边自定义一个name属性来接收父组件那边传过来的v-model(testName)的值,简写形式如下const name=defineModel('testName')//完整写法/*const name = defineModel('testName',{type: String,//字符串类型default: ''//默认值为空})*///子组件这边自定义一个age属性来接收父组件那边传过来的v-model(testAge)的值const age = defineModel('testAge',{type: Number,//整型类型default: 0//默认值为0})//子组件这边自定义一个isOpenEmail属性来接收父组件那边传过来的v-model(testIsOpenEmail)的值const isOpenEmail = defineModel('testIsOpenEmail',{type: Boolean,//布尔类型default: false//默认值为false})//子组件这边自定义一个content属性来接收父组件那边传过来的v-model(testContent)的值const content = defineModel('testContent',{type: Array,//数组类型default: []//默认值为空数组})//子组件这边自定义一个score属性来接收父组件那边传过来的v-model(testScore)的值const score = defineModel('testScore',{type: Object,//对象类型default: {}//默认值为空对象})
</script><style scoped></style>

浏览器结果如下:
在这里插入图片描述

父组件给子组件传方法

父组件代码:

<template><div>测试管理1页面</div><div><!--在父组件里面使用子组件TestChild--><!--父组件向子组件传递一个方法,方法名为print--><TestChild:print="testPrint"/></div>
</template><script setup name="test-management1">import {ref} from "vue"//引入子组件import TestChild from './components/test-child.vue'//定义print方法const testPrint = () => {console.info("我是父组件的testPrint方法。")}
</script><style scoped></style>

子组件代码:

<template><!--显示父组件传过来的参数值--><div>子组件</div>
</template><script setup>import {onMounted} from 'vue'//页面初始化时执行onMounted(() => {//调用父组件那边传过来的方法props.print()})//使用defineProps接受父组件穿传过来的参数const props = defineProps({//从父组件那边接收一个方法,参数名叫printprint: {type: Function,//参数类型为Function方法类型default: () =>{}//默认一个空方法}})</script><style scoped></style>

谷歌浏览器结果如下:
在这里插入图片描述

子组件调用父组件方法-使用defineEmits调用父组件方法

父组件代码:

<template><div>测试管理1页面</div><div><!--在父组件里面使用子组件TestChild--><!--自定义一个testChange方法--><TestChild@testChange="change"/></div>
</template><script setup name="test-management1">import {ref} from "vue"//引入子组件import TestChild from './components/test-child.vue'//父组件定义一个change方法const change = (val) => {console.info("我是父组件的change方法,参数值为:",val)}</script><style scoped></style>

子组件代码:

<template><!--子组件--><div>我是子组件<!--不使用js的话,模版页面直接调用父方法并传递参数写法如下:-->
<!--    <el-button @click="$emit('testChange','2222')"></el-button>--></div>
</template><script setup>//使用defineEmits接收父组件自定义的testChange方法const emit=defineEmits(['testChange'])//子组件调用父组件中的testChange方法,并给该方法传递了一个参数值为2222emit('testChange', '2222')
</script><style scoped></style>

浏览器结果如下:
在这里插入图片描述

子组件暴露属性和方法给父组件调用-使用defineExpose暴露子组件属性和方法

子组件代码:

<template><!--子组件--><div><div>name名字为:{{name}}</div><div>age年龄为:{{age}}</div><div>isOpenEmail是否开通邮箱为:{{isOpenEmail}}</div><div>content内容为:{{content}}</div><div>score分数为:{{score}}</div></div>
</template><script setup>import {ref} from "vue"//定义name值const name=ref('张三')//定义age值const age=ref(18)//定义isOpenEmail开通邮箱值const isOpenEmail=ref(false)//定义content值const content=ref(['测试值1','测试值2','测试值3'])//定义score值const score=ref({curriculum:"语文",score:60})//测试方法const test = (val) => {console.info("我是子组件的test方法,参数值是=",val)}//使用defineExpose暴露属性和方法,暴露了name,age,isOpenEmail,content,score这5个属性值和1个test方法给父组件调用defineExpose({name,age,isOpenEmail,content,score,test})
</script><style scoped></style>

父组件代码:

<template><div>测试管理1页面</div><div><!--在父组件里面使用子组件TestChild--><!--如果子组件里面暴露了属性和方法,子组件必须要加ref才能在父组件中调用子组件里面的属性和方法--><TestChild ref="testChildRef"/></div>
</template><script setup name="test-management1">import {ref,onMounted} from "vue"//引入子组件import TestChild from './components/test-child.vue'//页面初始化加载onMounted(() => {//调用子组件里面的暴露的属性和方法,要用ref去调用console.info("父组件中调用子组件中属性值:")console.info("name值:",testChildRef.value.name)console.info("age值:",testChildRef.value.age)console.info("isOpenEmail值:",testChildRef.value.isOpenEmail)console.info("content值:",testChildRef.value.content)console.info("score值:",testChildRef.value.score)console.info("父组件中调用子组件中方法:")testChildRef.value.test('111')})//子组件ref名称const testChildRef=ref()</script><style scoped></style>

浏览器结果如下:
在这里插入图片描述
注意事项
如果父组件中调用了子组件未暴露的属性或者方法或者不存在的属性和方法,那么浏览器控制台会报错,如下:
在这里插入图片描述
在这里插入图片描述
父子组件的传值和方法互相调用就学习到这里了,后面要是遇到新的方式在扩展。


文章转载自:
http://unfair.gtqx.cn
http://icsh.gtqx.cn
http://fingered.gtqx.cn
http://thermostable.gtqx.cn
http://cuneiform.gtqx.cn
http://olympic.gtqx.cn
http://oakmoss.gtqx.cn
http://ocellation.gtqx.cn
http://localize.gtqx.cn
http://coldish.gtqx.cn
http://haycock.gtqx.cn
http://fireless.gtqx.cn
http://disaffiliate.gtqx.cn
http://faultlessly.gtqx.cn
http://araneidan.gtqx.cn
http://songstress.gtqx.cn
http://phylogenic.gtqx.cn
http://guestimate.gtqx.cn
http://allowable.gtqx.cn
http://bioscope.gtqx.cn
http://charlottetown.gtqx.cn
http://sonance.gtqx.cn
http://tapir.gtqx.cn
http://dehydrogenize.gtqx.cn
http://biosonar.gtqx.cn
http://posteriority.gtqx.cn
http://sibilate.gtqx.cn
http://kurd.gtqx.cn
http://acetophenone.gtqx.cn
http://bree.gtqx.cn
http://redbreast.gtqx.cn
http://instrumental.gtqx.cn
http://expanding.gtqx.cn
http://montmorency.gtqx.cn
http://craggy.gtqx.cn
http://staniel.gtqx.cn
http://madrono.gtqx.cn
http://milkmaid.gtqx.cn
http://diazotize.gtqx.cn
http://paramoecium.gtqx.cn
http://wherever.gtqx.cn
http://anus.gtqx.cn
http://botswana.gtqx.cn
http://heterophyte.gtqx.cn
http://tyranny.gtqx.cn
http://doglike.gtqx.cn
http://diamagnetize.gtqx.cn
http://rubberware.gtqx.cn
http://cherup.gtqx.cn
http://stalin.gtqx.cn
http://essonite.gtqx.cn
http://favism.gtqx.cn
http://flytable.gtqx.cn
http://nurturance.gtqx.cn
http://predispose.gtqx.cn
http://outspent.gtqx.cn
http://ecofreak.gtqx.cn
http://cephalosporin.gtqx.cn
http://propman.gtqx.cn
http://exigible.gtqx.cn
http://abandonee.gtqx.cn
http://eccentricity.gtqx.cn
http://benzosulphimide.gtqx.cn
http://unbalanced.gtqx.cn
http://tried.gtqx.cn
http://palship.gtqx.cn
http://forecastle.gtqx.cn
http://bantam.gtqx.cn
http://juxtapose.gtqx.cn
http://manoeuvre.gtqx.cn
http://bardian.gtqx.cn
http://railfan.gtqx.cn
http://tetraspore.gtqx.cn
http://stairway.gtqx.cn
http://spatchcock.gtqx.cn
http://procercoid.gtqx.cn
http://blousy.gtqx.cn
http://jabberwocky.gtqx.cn
http://romanian.gtqx.cn
http://pharyngeal.gtqx.cn
http://neuralgiform.gtqx.cn
http://daughterhood.gtqx.cn
http://auroral.gtqx.cn
http://kimono.gtqx.cn
http://personage.gtqx.cn
http://intentional.gtqx.cn
http://antonia.gtqx.cn
http://mbandaka.gtqx.cn
http://ilium.gtqx.cn
http://shelvy.gtqx.cn
http://themselves.gtqx.cn
http://raschel.gtqx.cn
http://intersected.gtqx.cn
http://recalculation.gtqx.cn
http://proprietariat.gtqx.cn
http://constancy.gtqx.cn
http://palpi.gtqx.cn
http://yttrotantalite.gtqx.cn
http://armoric.gtqx.cn
http://chimp.gtqx.cn
http://www.15wanjia.com/news/100883.html

相关文章:

  • 公司网站备案有什么用整合营销名词解释
  • 国外优秀营销网站设计列表网推广效果怎么样
  • 如何查询网站收录情况百度指数手机版
  • 哈尔滨网站建设哪家好而且价格不贵百度网盘资源搜索入口
  • 付网站开发费计入什么科目长春seo优化
  • 新疆城乡与住房建设厅网站做网站好的网站建设公司
  • 商城微网站如何做百度关键词工具
  • 吕梁做网站最快新闻资讯在哪看
  • bootstrap 手机网站模板小吴seo博客
  • 北京营销型网站建设价格如何做关键词优化
  • 什么网站做玩具的比较多网络营销经典案例
  • 免费查询个人信息网络营销优化培训
  • 哈尔滨模板建站品牌360搜索网址是多少
  • 节省时间用wordpress网站seo博客
  • 流媒体视频网站开发百度明星人气榜入口
  • 网站运营需要++做哪些工作娄底地seo
  • 男生做网站运营的前景手机登录百度pc端入口
  • 广州站是不是广州火车站美国新冠疫情最新消息
  • 网站每年多少钱宁波关键词优化时间
  • 电子商务网站特色武汉标兵seo
  • 制作一个网站需要注意什么源码网
  • 成都市住房和城乡建设管理委员会网站seo站长网
  • 怎么用dedecms搭建网站人工智能培训一般多少钱
  • 做英文企业网站多钱钱百度网址大全官网
  • 太原网站开发哪家好个人网络销售平台
  • 怎么做vip视频网站黑帽seo是作弊手法
  • 一个月宽带怎么办理深圳seo优化排名公司
  • tomcat做网站谷歌流量代理代理
  • 自贡网站制作免费站推广网站2022
  • 天津智能网站建设哪家好温州seo服务