Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i read the contents from a text file as a string in Nuxt (or Vue)?

Id like to read the contents of a text file which i have imported in my .vue file like import ToS from '~/static/terms-of-service.txt';

I want to access the contents as a String.

How can I do it?

like image 747
sam k Avatar asked Oct 23 '17 18:10

sam k


People also ask

How do you use NUXT Vue?

In order to use Vue plugins we need to create a Nuxt plugin and we do this by creating a . js file in the /plugins folder. We then paste in the code that we need to use the plugin. We import Vue and VTooltip and then tell Vue to use the VTooltip.

Does NUXT include Vue?

By default, all Nuxt. js pages are using the default. vue layout. Vuex in Nuxt.

Is NUXT vue3?

It's only right that Nuxt 3 was built to support Vue 3 features. Vue 3 was released in October 2020 and has since garnered a lot of praise from the Vue community. With Nuxt 3 being written in Vue 3, you get access to features like the Composition API, better module imports, and overall improved app performance.


2 Answers

VUE CLI 3

  1. First install the raw loader npm install raw-loader --save-dev

  2. If you don't have a vue.config.js, make one at root and add

    module.exports = {
      chainWebpack: config => {
        config.module
          .rule('raw')
          .test(/\.txt$/)
          .use('raw-loader')
          .loader('raw-loader')
          .end()
      }
    }
    
  3. To get the text file as string (if placed in src/assets/txt/): import file from '@/assets/txt/file.txt'

N.B. Remember to rebuild

like image 99
H Johnson Avatar answered Sep 20 '22 20:09

H Johnson


I'm using nuxt (created with vue cli 3), and this is what worked for me:

  1. npm install --save-dev raw-loader
  2. Update nuxt.config.js
  build: {
    /*
    ** You can extend webpack config here
    */
    extend (config, ctx) {

      config.module.rules.push({
        enforce: 'pre',
        test: /\.txt$/,
        loader: 'raw-loader',
        exclude: /(node_modules)/
      });

    }
  }
like image 35
RoccoB Avatar answered Sep 20 '22 20:09

RoccoB