• 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. })

  • 相关阅读:
    CentOS 7 安装&卸载 MySQL 8 详细图文教程
    【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
    Python、设计原则和设计模式-对象行为类设计模式(一)
    京都大学利用 CNN 预测粮食产量,丰收不问天,问 AI 就够了
    fastadmin框架调用model层的方法
    【算法】中缀表达式转成后缀表达式(逆波兰表达式)再计算得出结果
    [附源码]Python计算机毕业设计Django基于Java的日用品在线电商平台
    【负荷预测】基于自适应灰狼算法(IGWO)改进LSSVM实现负荷预测附Matlab代码
    前端核武器:开源FrontendBlocks所见即所得编辑器让所有人都能做前端布局
    一文学会使用Bazel构建C++项目
  • 原文地址:https://blog.csdn.net/qq_59626859/article/details/133762916