Read yaml file with vue.js from disk (not URL)

Viewed 3138

In Vue.js I would like to read a YAML file from my filesystem (this file is not available online so can not be requested via URL), convert it to JSON and somehow expose it in a way that all components can access it. How can I do it?

I should probably use js-yaml but I can't find a way for it to work in Vue.js.

1 Answers

I may be missing the problem, but

npm install yaml-js --save

then import into component and use

<script>
  import { yaml } from 'yaml-js' 
  // or maybe the following, try both
  import yaml from 'yaml-js'

Your biggest problem would be the location of the Yaml file to be read in. I'm reading files from the static folder, so if you can locate it there you should be able to read it with

return Vue.http.get('Yaml.whatever')
  .then(response => {
    // process response.body

In main.js set up the root path (must be within your web site)

import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root = './static/'
Related