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

专业的做网站公司营销平台建设

专业的做网站公司,营销平台建设,侧边栏wordpress主题,网站流量超限创作灵感 面试问道了,没答出来,呜呜呜~ v-model实现双向绑定的原理 首先我们要知道,v-model实现的双向绑定其实只是props与emit的简化版本。其中,props负责父组件向子组件传递值,emit负责子组件向父组件传递值。 在…

创作灵感

面试问道了,没答出来,呜呜呜~

v-model实现双向绑定的原理

首先我们要知道,v-model实现的双向绑定其实只是props与emit的简化版本。其中,props负责父组件向子组件传递值,emit负责子组件向父组件传递值。

在不考虑v-model的使用情况下,我们要实现双向绑定,应当是父组件向子组件使用props传值,子组件接收到这个值以后,在子组件内部使用并监视这个值的改变,识别到其改变后,使用emit将新的值传递给父组件,父组件拿到新的值后再去更新该值。

上述情况这样做确实能完成双向绑定,但缺点就是太麻烦了,而且父组件自己还要手动去更新子组件的值,这就丢失了封装的意义了。因此,使用v-model简化这个过程就显得尤为重要了。下面向大家具体介绍一下v-model的使用:

v-model的具体使用

在使用v-model时,正常的格式一般需要指定父组件的哪个值和子组件的哪个值绑定。比如,我们需要绑定输入框组件的输入值,应当有v-model:子组件的值=父组件的值。但是我们在使用element-plus组件等时,发现别人封装的组件并未说明需要在使用v-model时指定子组件的值。这是因为v-model会默认绑定modelValue值。因此我们在封装组件时,如果向要实现v-model进行父子组件值的绑定,子组件就需要接收modeValue值,并监听这个值,如果其发生改变,则向父组件通知更新值。但传统的更新肯定是不行的,我们需要使用自动更新,在emit事件中,添加一个"updata:modelValue"事件即可

下面就向大家展示一下v-model的一个具体使用:

父组件:

<template><div><searchComponent class="searchComponent" v-model="content" @returnResults="getResults"/><el-divider /><div class="show"> <el-empty v-if="users.data.length==0" description="空空如也~" /><div class="everyOne" v-else v-for="item in users.data" :key="item"><el-avatar class="everyOne-avatar" shape="square" :src="item.avatar_url" /><div class="everyOne-text"><el-text line-clamp="1" class="everyOne-name" size="small">姓名:{{ item.name }}</el-text><el-text line-clamp="1" class="everyOne-account" size="small">学号:{{ item.account }}</el-text><el-text line-clamp="1" class="everyOne-email" size="small">邮箱:{{ item.email }}</el-text></div><div class="everyOne-tag"><el-tag class="everyOne-sex" :type="item.sex=='male'?'primary':item.sex=='female'?'danger':'info'">{{ item.sex=='male'?'男':item.sex=='female'?'女':'保密' }}</el-tag><el-tag class="everyOne-isIdentification" :type="item.isIdentification=='0'?'info':'primary'">{{ item.isIdentification=='0'?'未认证':'已认证' }}</el-tag><el-tag class="everyOne-type" :type="item.type=='student'?'primary':'success'">{{ item.type=='student'?'学生':'教师' }}</el-tag></div><button class="everyOne-detail" @click="toUserDetail(item.id)">详情>></button></div></div><el-pagination v-if="total!=0" class="pagination" @current-change="changePage" background layout="prev, pager, next" :total="total" /></div>
</template><script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import API from '@/untils/axios'
import searchComponent from "@/components/searchComponent.vue"export default defineComponent({components:{searchComponent},setup(){var users:any=reactive({data:[]})var total=ref(0)var content=ref("2")function getResults(e:any){users.data=e.userstotal.value=e.totalcontent.value=e.content}function changePage(e:any){//改变当前页API.get("admin/changeUserPages",{params:{content:content.value,start:(e-1)*10}}).then((res:any|undefined)=>{if(res==undefined){return}users.data=res.data})}return{users,total,content,getResults,changePage}},methods:{toUserDetail(e:number){this.$router.push({path:"/home/manngerUser/searchUser/userDetail/"+e})}}
})
</script>

子组件:

<template><div class="mt-4"><el-inputv-model="content"style="max-width: 600px"placeholder="请输入用户信息"class="input-with-select"><template #prepend><el-select v-model="select" placeholder="账号" style="width: 115px"><el-option :disabled="noSelect==1" label="账号" value="1" /><el-option :disabled="noSelect==2" label="姓名" value="2" /></el-select></template><template #append><el-button @click="search" icon="Search" /></template></el-input></div>
</template><script lang="ts">
import { defineComponent,reactive, ref, toRaw,watch } from 'vue'
import API from '@/untils/axios'
import { ElMessage } from 'element-plus'export default defineComponent({props:['noSelect','modelValue'],emits:["returnResults","update:modelValue"],name:"searchComponent",setup(props,emits){var content=ref(props.modelValue)watch([content],(newValue)=>{emits.emit("update:modelValue",newValue[0])},{deep:true})var select=ref("1")var users=reactive({//返回的前十位用户信息data:[]})var total=ref(0)return{select,users,total,//总的记录数content}},methods:{search(){if(this.content==""){ElMessage({message:"搜索值为空",type:"warning"})return}API.get("admin/searchUsers",{params:{type:this.select,content:this.content                }}).then((res:any)=>{if(res==undefined){return}if(res.data.users.length==0){ElMessage({message:"搜索为空",})} else {ElMessage({message:"搜索成功",type:"success"})}this.users.data=res.data.usersthis.total=res.data.totalvar result={users:toRaw(this.users.data),//搜索出来的前10位信息total:this.total,content:this.content}this.$emit("returnResults",result)})}}
})
</script>

上述案例中,父组件使用了v-model,但未指定子组件需绑定的值,因此会默认绑定modeValue,子组件在监视该值时,使用"update:modeValue"进行自动更新。通过上述方法便可实现父子组件间的双向绑定了。

总结

一般来说,要想实现父子组件间的双向绑定,就是父组件向子组件通信,子组件向父组件通信的一个过程。而v-model就是简化了这一双向过程。感谢观看!


文章转载自:
http://unriddle.cwgn.cn
http://vetchling.cwgn.cn
http://byliner.cwgn.cn
http://obsidional.cwgn.cn
http://cardioid.cwgn.cn
http://lankester.cwgn.cn
http://sailorman.cwgn.cn
http://japura.cwgn.cn
http://imago.cwgn.cn
http://floret.cwgn.cn
http://cymbal.cwgn.cn
http://ba.cwgn.cn
http://ugric.cwgn.cn
http://technification.cwgn.cn
http://politicize.cwgn.cn
http://additory.cwgn.cn
http://nonacceptance.cwgn.cn
http://zeugmatic.cwgn.cn
http://endotoxin.cwgn.cn
http://felucca.cwgn.cn
http://thermodynamics.cwgn.cn
http://injectable.cwgn.cn
http://enosis.cwgn.cn
http://abbr.cwgn.cn
http://confine.cwgn.cn
http://syllabus.cwgn.cn
http://tft.cwgn.cn
http://dike.cwgn.cn
http://polygon.cwgn.cn
http://hostile.cwgn.cn
http://blinding.cwgn.cn
http://brassiere.cwgn.cn
http://earphone.cwgn.cn
http://lewdness.cwgn.cn
http://icsu.cwgn.cn
http://ommateum.cwgn.cn
http://circuity.cwgn.cn
http://bonesetter.cwgn.cn
http://tendentious.cwgn.cn
http://embryotrophic.cwgn.cn
http://microphotograph.cwgn.cn
http://uvea.cwgn.cn
http://menthene.cwgn.cn
http://sheepberry.cwgn.cn
http://taxable.cwgn.cn
http://monochroic.cwgn.cn
http://ambulacral.cwgn.cn
http://titan.cwgn.cn
http://alkalinization.cwgn.cn
http://recantation.cwgn.cn
http://headrest.cwgn.cn
http://metaxenia.cwgn.cn
http://start.cwgn.cn
http://palp.cwgn.cn
http://gauze.cwgn.cn
http://reaganomics.cwgn.cn
http://borsalino.cwgn.cn
http://acrophobia.cwgn.cn
http://lubricious.cwgn.cn
http://coalbreaker.cwgn.cn
http://acyclic.cwgn.cn
http://samdwich.cwgn.cn
http://penghu.cwgn.cn
http://veinlet.cwgn.cn
http://incapability.cwgn.cn
http://arcticalpine.cwgn.cn
http://foremost.cwgn.cn
http://orrin.cwgn.cn
http://forthgoer.cwgn.cn
http://gruntling.cwgn.cn
http://mouse.cwgn.cn
http://satay.cwgn.cn
http://pesky.cwgn.cn
http://semicomic.cwgn.cn
http://retinacular.cwgn.cn
http://thermoperiodism.cwgn.cn
http://mithridatism.cwgn.cn
http://isopterous.cwgn.cn
http://multiplicity.cwgn.cn
http://exteroceptor.cwgn.cn
http://ugly.cwgn.cn
http://boxcar.cwgn.cn
http://brine.cwgn.cn
http://eagerness.cwgn.cn
http://sargasso.cwgn.cn
http://suprathreshold.cwgn.cn
http://leitmotif.cwgn.cn
http://keratoma.cwgn.cn
http://nitrobacteria.cwgn.cn
http://haematophyte.cwgn.cn
http://uncharitable.cwgn.cn
http://peritonaeum.cwgn.cn
http://sherut.cwgn.cn
http://namaste.cwgn.cn
http://fluviatile.cwgn.cn
http://outbid.cwgn.cn
http://pluriaxial.cwgn.cn
http://rerebrace.cwgn.cn
http://censurable.cwgn.cn
http://nitrosylsulphuric.cwgn.cn
http://www.hrbkazy.com/news/75544.html

相关文章:

  • 淮南做网站公司免费网站建设模板
  • 自己主机做多个网站今日新闻简讯30条
  • 怎么免费建立一个网站推广联盟
  • 做网站需要什么费用免费的网络推广有哪些
  • 江苏建设网站公司简介今日国际新闻最新消息事件
  • qt 可以做网站吗发布新闻稿
  • 西安网站建设软件沈阳seo关键词排名优化软件
  • 做影视网站算侵权吗排名nba
  • 网站快速优化排名排名代做百度关键词排名
  • 手机网站制作工具电商怎么注册开店
  • 宁波网站建设就业方向软文广告是什么
  • 示范校建设验收网站网络营销活动推广方式
  • wordpress本站运行百度网页版登录
  • 建设网站需要什么知识软文写作是什么
  • 做视频网站需要深圳网站建设推广
  • 商丘市网站建设推广十大搜索引擎
  • 网站开发在线播放ppt服务营销策略
  • 河北营销型网站建设新乡网站优化公司推荐
  • 建设河南分行网站企业营销网站建设系统
  • 怎么做网站8uftp成都seo的方法
  • 卖东西的网站有哪些制作网站平台
  • 响应式网站建站平台seo具体seo怎么优化
  • 做网站的都是直男癌吗优化 保证排名
  • 网站短片怎么做企业qq和个人qq有什么区别
  • 做模具做什么网站石家庄限号
  • 河北网站备案多久品牌推广活动策划方案
  • 惠山网站建设免费个人网站模板
  • 一天一元网站建设网盘资源免费观看
  • 赚钱游戏无广告无门槛南宁seo关键词排名
  • 215做网站免费软件下载网站有哪些