为什么未分配的Vue实例不收集垃圾? [英] Why doesn't an unassigned Vue instance get garbage collected?
问题描述
这是在HTML页面上使用VueJS的标准方法(无捆绑包).没有分配.
Here's the standard way to use VueJS on the HTML page (without bundles). No assignment.
<script>
new Vue({
el: '#root',
data: {
title: 'Hello'
}
});
</script>
为什么垃圾收集器不收集此Vue对象?
Why Garbage Collector doesn't collect this Vue object?
推荐答案
实例化Vue对象时,它实际上将自身安装到DOM元素(此处为#root
元素),如本文档页面 Vue实例>实例生命周期挂钩.
When you instantiate a Vue object, it actually mounts itself to the DOM element, here #root
element, as briefly hinted in this documentation page The Vue Instance > Instance Lifecycle Hooks.
通过在浏览器中使用开发者工具(例如在Chrome中),您可以打开控制台标签并提示输入console.log(window.$vm0);
并按Enter.即使没有将其分配给变量,您也可以访问Vue运行时实例:
By using Developer Tools in your browser, like in Chrome, you can open the console tab and prompt, type console.log(window.$vm0);
and hit enter. And you get access to your Vue runtime instance even it was not assigned to a variable:
> Vue {_uid: 2, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: Vue, …}
我在 要回答当前问题,主要要点是,实际上Vue本身在幕后发生了变量赋值/DOM安装,因此这就是为什么垃圾收集不会触发的原因.
PS.关于处理垃圾回收,有详细的文档文章避免内存泄漏在Vue应用程序中.
PS. There is a detailed documentation article Avoiding Memory Leaks in relation to handling Garbage Collection in a Vue application.
这篇关于为什么未分配的Vue实例不收集垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!