web123456

vue watch error: Error in callback for watcher "xxx": "TypeError: Cannot read properties of undefined ...

Table of contents

1. Problem

2. Solution

3. Summary


1. Problem

1. After listening to an object in data, it actually reported an error. The error is strange: you cannot use the apply method on undefined

1) The detailed errors are as follows:

?9b69:5059 [Vue warn]: Error in callback for watcher "flowCategory": "TypeError: Cannot read properties of undefined (reading 'apply')"

found in

---> <FlowItem> at src/projects/comen/implemenceWorkstation/flowSetting/
       <ElTabPane> at packages/tabs/src/
         <ElTabs> at packages/tabs/src/
           <View> at src/views/implemenceWorkstation/flowSetting/
             <Index> at src/layout/
               <App> at src/
                 <Root>
warn$2 @ ?9b69:5059
logError @ ?9b69:3728
globalHandleError @ ?9b69:3724
handleError @ ?9b69:3691
invokeWithErrorHandling @ ?9b6

2) The code is as follows:

  watch: {
    flowCategory: {
      hanlder(newval, oldval) {
        ('flowCategory', newval, oldval);
      },
      deep: true
    }
  }

2. Solution

1. Is it okay to see the above code? I am also very confident. Isn’t it just that the values ​​before and after the change were printed in the handler function? This is how I write it, and I am so confident.

2. I found it after searching for a long timeI wrote the word handler wrongly, if you type incorrectly, it was written as 'hanlder', there was no prompt for ide &***&

Change it to the following

  watch: {
    flowCategory: {
      handler(newval, oldval) {
        ('flowCategory', newval, oldval);
      },
      deep: true
    }
  }

3. I finally understand nowError: It is to tell us that there is a problem with the handler. Watch doesn't know what to do when listening to changes.

3. Summary

1.Report the above error, which means there is a problem with the handler function.. You can check the following points:

  1) Is the handler function written?

  2) Is the handler spelled correctly (Although it's a bit stupid, I'm really because of this today...)

   3) The handler function cannot be an arrow function(After this pointing changes, apply is also problematic)

2. Ah, stupid me.

/*

Hope it helps you!

If there are any errors, please correct me! Thank you so much!

*/