简体   繁体   English

如何在现有的 vuejs 项目中集成 requirejs?

[英]How to integrate requirejs in existing vuejs project?

I'm trying to load JSON data inside my vue template.我正在尝试在我的 vue 模板中加载 JSON 数据。
To fetch JSON data, I'm using the require instead of import .为了获取 JSON 数据,我使用了require而不是import So I can make set any dynamic UUID in the future for local JSON file name.所以我可以在未来为本地 JSON 文件名设置任何动态 UUID。 But this resulting to error of error 'require' is not defined但这导致error 'require' is not defined

<script>
export default {
    name: "BloePage",
    data() {
        return {
            blogcontent: require(`../content_files/UUDD_WWAA_EEFF_EWWW_AAWW.json`)
        }
    }
}
</script>

As I got to know from web sources.正如我从网络资源中了解到的那样。 To use require in vuejs project.在 vuejs 项目中使用require I need integrate requirejs in my existing vuejs project.我需要在我现有的 vuejs 项目中集成requirejs I searched for this a lot.我搜索了很多。 but didn't found any straight forward way of doing this...但没有找到任何直接的方法来做到这一点......

Any Suggestions or Solution?任何建议或解决方案?

Node.js's require and RequireJS do not work the same way. Node.js 的require和 RequireJS 的工作方式不同。 You cannot use RequireJS to load JSON, synchronously, from a URL.您不能使用 RequireJS 从 URL 同步加载 JSON。

If you want to fetch data from a URL, use Ajax.如果要从 URL 获取数据,请使用 Ajax。

You can find an example of how to do this in the Vue manual :您可以在 Vue 手册中找到如何执行此操作的示例:

new Vue({
  el: '#app',
  data () {
    return {
      blogcontent: null
    }
  },
  mounted () {
    axios
      .get('../content_files/UUDD_WWAA_EEFF_EWWW_AAWW.json')
      .then(response => (this.blogcontent = response))
  }
})

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

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