简体   繁体   English

VueJS:如何在模板中使用main.js中的数据?

[英]VueJS: How do I use data from main.js in my templates?

I have my Vue scope in my main.js: 我的main.js中有Vue范围:

new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App },
  data: {
    name: 'Levi'
  }
})

and I want to use it in my template: test.vue : 我想在我的模板中使用它: test.vue

<p>My name is {{ name }}

I'm using vue-router for all the routing functionality, and that all works fine. 我将vue-router用于所有路由功能,并且一切正常。 But when I try calling name in my template I get 但是当我尝试在模板中调用name ,我得到了

[Vue warn]: Property or method "name" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

只需在我的模板中添加脚本标签并使用module.exportsexport default

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM