Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

vue.js - Vue 3 - number 0 increase (++) is not 1 but 101 in template , why?

Vue.createApp({
  data() {
    return {
      counter: 0
    }
  },
  template: '<div>{{counter++}}</div>'
}).mount('#root')

Finally, this code shows 101 on the page. Does someone know the detail about this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. The reason your counter variable is rendered as 101 and not 1 is because you have a side-effect in your template. When template is rendering the counter is incremented, Vue registers the change and trigger rendering again ...it is simply an infinite loop

  2. The reason this infinite loop is not so infinite but stops after 101 iterations lies in the fact that Vue internally checks for such situation and throws an error (which you should see in browser dev tools console). Condition for throwing an error is number_of_iterations > RECURSION_LIMIT where RECURSION_LIMIT is set to 100

Error:

Maximum recursive updates exceeded. This means you have a reactive effect that is mutating its own dependencies and thus recursively triggering itself. Possible sources include component template, render function, updated hook or watcher source function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...