简体   繁体   中英

How to integrate requirejs in existing vuejs project?

I'm trying to load JSON data inside my vue template.
To fetch JSON data, I'm using the require instead of import . So I can make set any dynamic UUID in the future for local JSON file name. But this resulting to error of 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. I need integrate requirejs in my existing vuejs project. 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. You cannot use RequireJS to load JSON, synchronously, from a URL.

If you want to fetch data from a URL, use Ajax.

You can find an example of how to do this in the Vue manual :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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