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

Vue2和Vue3的emit、props、watch等知识点对比

1.props/defineProps
使用场景:

       一般当父组件需要给子组件传值的时候会用到。

        Vue2:props

vue2在父组件里引入子组件以后需要在components里面注册后再使用;

父组件
<son-compnents :info=info></son-components>import SonCompnents from "./cpmponents/SonCompnents.vue"components: {SonCompnents,},data(){return {info:'个人信息'}}
子组件
props:['info'] //数组写法
props:{info:Stirng  //对象写法
}
Vue3:defineProps

vue3的父组件引用后直接使用即可;

父组件
<son-compnents :info=info></son-components>import SonCompnents from "./cpmponents/SonCompnents.vue"
import {ref }from 'vue'
let info = ref('props传参')
子组件
<script setup>
import {ref,computed} from 'vue'
const props = defineProps({info:String
})
一般props的值就用computed来接收使用
let getInfo = computed(()=>{return props.info
})
</script>
2. emit/defineEmits
使用场景:

用于子组件向父组件传递信息,修改父组件传的props的值

Vue2:emit
子组件
<button @click="sendMes(name)">点击向父组件传值</button>data(){return{name:'子组件'}}methods:{sendMes(name){this.$emit('getMes',name)   触发父组件绑定的getMes事件从而触发对应的方法,后面是传的参数}
}
父组件<son-componet @getMes="changeMes"></son-componet>import SonComponet from '@/components/SonComponet'data(){return(){name:'原本的值'}}methods:{changeMes(name){this.name=name}}
Vue3:defineEmits
子组件
<el-button @click="sendMes">子组件给父组件传值</el-button>const emits = defineEmits(["close"]);
const sendMes = ()=>{emits("close","传的参数") //可传可不传
}
父组件<son-component @close= "changeMes"></son-component>import SonComponent from '@/components/SonComponent.vue'
import {ref} from 'vue'let mes = ref('原值')
const changeMes = (name)=>{mes.value = name }
3.watch
使用场景:

用于对数据的监听并实时做出处理;

Vue2:watch
子组件
1.监听对象的一个属性值并实时修改另一个值watch: {'form.currency': {handler(newValue, oldValue) {if (newValue == 'USD') {this.currencyType = '万美元';} else {this.currencyType = '万元';}},deep: true,},'form2.currency': {handler(newValue, oldValue) {if (newValue == 'USD') {this.currencyType = '万美元';} else {this.currencyType = '万元';}},deep: true}},
Vue3:watch

这里的场景是子组件是弹窗,父组件是表格,在表格中引入,点击编辑,子组件弹出框的数据随之更改;

子组件
import {ref,watch} from 'vue'const props = defineProps({info:Obeject
})
let info = ref({})当监听的值是一个对象的某个属性值时,watch的第一个参数就需要写成函数,其他直接写参数即可 
watch(()=>props.info   //如果只是子组件本身的一个值 name,(newValue,oldValue)=>{if(newValue){form = {name = newValue.nameprice= newValue.price}} },{     第三个对象配置watch的属性deep:true}
)
Vue3:watchEffect 

 watchEffect的作用是不需要指定需要监听的属性,而是监听一个回调,当回调内所使用的数据发生变化时,回调就会执行;

缺点:1.不会立刻执行(可用immediate)解决;2.获取不到newValue和oldValue;

更多知识点得参考其他教程

import {watchEffect,ref} from 'vue'
let a = ref(1)
let b = ref(1)const stop  =watchEffect(()=>{console.log(a,b) 一但a或b的值发生了改变,这个打印函数就会执行 })

http://www.lryc.cn/news/188814.html

相关文章:

  • HTML 笔记:初识 HTML(HTML文本标签、文本列表、嵌入图片、背景色、网页链接)
  • 使用弹性盒子flex对html进行布局和动态计算视口高度
  • 华为云云耀云服务器L实例评测|华为云耀云服务器L实例评测用例(五)
  • uniapp-vue3微信小程序实现全局分享
  • Qt如何实现动态背景-视频背景
  • vue按键全屏和F11全屏共存
  • springboot就业信息管理系统springboot32
  • 深入探讨芯片制程设备:从原理到实践
  • Vuex的简介以及入门案例
  • 上海亚商投顾:沪指探底回升 华为汽车概念股集体大涨
  • Android网络监听
  • Kubernetes 常用命令 持续更新
  • 达梦数据库常用命令行
  • 【通信系列 6 -- AT 命令介绍】
  • flask捕获@app.errorhandler/@app.after_request全局异常总结
  • 智能晾衣架丨以科技解放双手
  • asp.net饭店订餐管理系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio计算机设计定制
  • Pushgateway的场景使用
  • 时间范围配置(昨天,今天,本周,本月,本季度,本年)
  • PlantUML 绘图
  • spring boot自定义配置时在yml文件输入有提示
  • Linux:I/O 5种模型
  • 企业提升效率的秘密武器,推荐J2L3x聊天软件!
  • Android Termux安装MySQL,并使用cpolar实现公网安全远程连接[内网穿透]
  • 如何有效管理公司分配给员工的个人微信,实现聚合聊天管理?
  • 【HTML5】语义化标签记录
  • 数据结构———— 堆
  • 如何选择专业的游戏开发公司?
  • Unity基础课程之物理引擎3-碰撞检测案例-吃金币并加分显在UI文本框上
  • DAPLink V2 STLink V2 JLink V9.7 串口四合一仿真调试下载器