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


    1.props/defineProps
    使用场景:

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

            Vue2:props

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

    1. 父组件
    2. import SonCompnents from "./cpmponents/SonCompnents.vue"
    3. components: {
    4. SonCompnents,
    5. },
    6. data(){
    7. return {
    8. info:'个人信息'
    9. }
    10. }
    1. 子组件
    2. props:['info'] //数组写法
    3. props:{
    4. info:Stirng //对象写法
    5. }
    Vue3:defineProps

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

    1. 父组件
    2. import SonCompnents from "./cpmponents/SonCompnents.vue"
    3. import {ref }from 'vue'
    4. let info = ref('props传参')
    1. 子组件
    2. emit/defineEmits
    使用场景:

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

    Vue2:emit
    1. 子组件
    2. data(){
    3. return{
    4. name:'子组件'
    5. }
    6. }
    7. methods:{
    8. sendMes(name){
    9. this.$emit('getMes',name) 触发父组件绑定的getMes事件从而触发对应的方法,后面是传的参数
    10. }
    11. }
    1. 父组件
    2. "changeMes">
    3. import SonComponet from '@/components/SonComponet'
    4. data(){
    5. return(){
    6. name:'原本的值'
    7. }
    8. }
    9. methods:{
    10. changeMes(name){
    11. this.name=name
    12. }
    13. }
    Vue3:defineEmits
    1. 子组件
    2. "sendMes">子组件给父组件传值
    3. const emits = defineEmits(["close"]);
    4. const sendMes = ()=>{
    5. emits("close","传的参数") //可传可不传
    6. }
    1. 父组件
    2. "changeMes">
    3. import SonComponent from '@/components/SonComponent.vue'
    4. import {ref} from 'vue'
    5. let mes = ref('原值')
    6. const changeMes = (name)=>{
    7. mes.value = name
    8. }
    3.watch
    使用场景:

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

    Vue2:watch
    1. 子组件
    2. 1.监听对象的一个属性值并实时修改另一个值
    3. watch: {
    4. 'form.currency': {
    5. handler(newValue, oldValue) {
    6. if (newValue == 'USD') {
    7. this.currencyType = '万美元';
    8. } else {
    9. this.currencyType = '万元';
    10. }
    11. },
    12. deep: true,
    13. },
    14. 'form2.currency': {
    15. handler(newValue, oldValue) {
    16. if (newValue == 'USD') {
    17. this.currencyType = '万美元';
    18. } else {
    19. this.currencyType = '万元';
    20. }
    21. },
    22. deep: true
    23. }
    24. },
    Vue3:watch

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

    1. 子组件
    2. import {ref,watch} from 'vue'
    3. const props = defineProps({
    4. info:Obeject
    5. })
    6. let info = ref({})
    7. 当监听的值是一个对象的某个属性值时,watch的第一个参数就需要写成函数,其他直接写参数即可
    8. watch(
    9. ()=>props.info //如果只是子组件本身的一个值 name,
    10. (newValue,oldValue)=>{
    11. if(newValue){
    12. form = {
    13. name = newValue.name
    14. price= newValue.price
    15. }
    16. }
    17. },
    18. { 第三个对象配置watch的属性
    19. deep:true
    20. }
    21. )
    Vue3:watchEffect 

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

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

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

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

  • 相关阅读:
    大数据-hadoop环境安装(集群)
    java三层架构/表现层-业务层-持久层
    在 10 分钟内在 Remix (React) 中实现单点登录
    3Darray 修改array值然后保存图片
    PS插件一键轻松搞定电商产品摄影图!
    springboot实现支付宝支付功能
    【人话版】WEB3黑暗森林中的隐私博弈
    验证码(easy-captcha)
    kotlin图片合成和压缩
    数据科学中常用的应用统计知识
  • 原文地址:https://blog.csdn.net/qq_59626859/article/details/133762916