为什么不能在 vue 模板中使用 window ? [英] why can't use window in vue template?
问题描述
我在 window 对象中设置了一个属性以供全局使用,如下所示:
i set a property in window Object for using it globally, like this:
window.configs.foo=true;
但是当像这样使用时:
<v-btn v-if="window.configs.foo">go</v-btn>
在控制台中,我收到此错误:
in console, i get this error:
[Vue 警告]:实例上未定义属性或方法window"但在渲染期间引用.确保这个属性是反应式,无论是在数据选项中,还是对于基于类的组件,通过初始化属性.
[Vue warn]: Property or method "window" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
<小时>
如何在 vueJs 模板中使用 window 对象?
推荐答案
嗯,替代方案是在 Vue 中使用 $root.在你的 vue 实例中定义 foo
,它将在所有带有 this.$root.foo
的组件中可用.
Well, Alternate is to use $root in Vue. Define foo
at your vue instance and it will be available in all the component with this.$root.foo
.
这是官方文档
希望能帮到你
这篇关于为什么不能在 vue 模板中使用 window ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!