【问题标题】:How can i read the contents from a text file as a string in Nuxt (or Vue)?如何在 Nuxt(或 Vue)中将文本文件中的内容作为字符串读取?
【发布时间】:2018-04-04 09:41:51
【问题描述】:

我想阅读我在 .vue 文件中导入的文本文件的内容,例如 import ToS from '~/static/terms-of-service.txt';

我想以字符串的形式访问内容。

我该怎么做?

【问题讨论】:

  • 您使用的构建系统是什么?

标签: vuejs2 vue-component nuxt.js


【解决方案1】:

VUE CLI 3

  1. 首先安装原始加载程序 npm install raw-loader --save-dev

  2. 如果您没有 vue.config.js,请在根目录下创建一个并添加

    module.exports = {
      chainWebpack: config => {
        config.module
          .rule('raw')
          .test(/\.txt$/)
          .use('raw-loader')
          .loader('raw-loader')
          .end()
      }
    }
    
  3. 以字符串形式获取文本文件(如果放在 src/assets/txt/ 中): import file from '@/assets/txt/file.txt'

注意记得重建

【讨论】:

    【解决方案2】:

    我正在使用 nuxt(使用 vue cli 3 创建),这对我有用:

    1. npm install --save-dev raw-loader
    2. 更新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)/
          });
    
        }
      }
    

    【讨论】:

      【解决方案3】:

      我最终使用了https://github.com/webpack-contrib/raw-loader

      看来你需要一个loader来读取vue中的文件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-02
        • 1970-01-01
        相关资源
        最近更新 更多