Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

221. 踩坑:vue array push 导致的内存泄漏 #221

Open
ly525 opened this issue Dec 14, 2018 · 0 comments
Open

221. 踩坑:vue array push 导致的内存泄漏 #221

ly525 opened this issue Dec 14, 2018 · 0 comments

Comments

@ly525
Copy link
Owner

ly525 commented Dec 14, 2018

{{ arr }}
<v-btn @click.native="add">add</v-btn> 
<v-btn @click.native="change">change</v-btn>
data () {
 return {
  arr: [],
 },
},
methods: {
  add () {
    this.arr.push({
      a: 10,
    });
  },
  change () {
    this.arr[0]['a'] = 100;
  }
}

在 Chrome devtool 中使用 source 查看 vue.js 中的 walk 函数,会发现add 里面push 进去的 a 会被walk,然后被watch,导致内存狂涨

总结

在vue 中,除非需要在template 中更新的变量,否则一般情况下不要声明在 data 中,可以定义在模块内变量或者声明该变量不触发 vue 的响应式更新。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant