【发布时间】:2017-04-28 20:41:38
【问题描述】:
我有一个非常基本的 Vue.js 应用程序,如下所示:
index.html(为了简洁起见,只是 <body>)
<div id="root"></div>
main.js
var config = {
title: 'My App',
}
new Vue({
el: '#root',
template: '<div>{{ config.title }}</div>',
})
这给了我:
Property or method "config" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.
(found in root instance)
我猜这是因为 Vue 在数据存储中而不是 window 中寻找它。有什么方法可以表明config 是这里的全局变量,还是有更好的方法来完全做到这一点?我想我可以将配置对象作为道具传递给我的根 Vue 实例,但似乎只有组件接受道具。感谢您对此提供任何见解。
【问题讨论】:
-
为什么不直接把它传给 Vue 实例呢?
data : { config : config },
标签: javascript vue.js