【发布时间】:2018-04-04 09:41:51
【问题描述】:
我想阅读我在 .vue 文件中导入的文本文件的内容,例如 import ToS from '~/static/terms-of-service.txt';
我想以字符串的形式访问内容。
我该怎么做?
【问题讨论】:
-
您使用的构建系统是什么?
标签: vuejs2 vue-component nuxt.js
我想阅读我在 .vue 文件中导入的文本文件的内容,例如 import ToS from '~/static/terms-of-service.txt';
我想以字符串的形式访问内容。
我该怎么做?
【问题讨论】:
标签: vuejs2 vue-component nuxt.js
VUE CLI 3
首先安装原始加载程序
npm install raw-loader --save-dev
如果您没有 vue.config.js,请在根目录下创建一个并添加
module.exports = {
chainWebpack: config => {
config.module
.rule('raw')
.test(/\.txt$/)
.use('raw-loader')
.loader('raw-loader')
.end()
}
}
以字符串形式获取文本文件(如果放在 src/assets/txt/ 中):
import file from '@/assets/txt/file.txt'
注意记得重建
【讨论】:
我正在使用 nuxt(使用 vue cli 3 创建),这对我有用:
npm install --save-dev raw-loadernuxt.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)/
});
}
}
【讨论】:
我最终使用了https://github.com/webpack-contrib/raw-loader
看来你需要一个loader来读取vue中的文件
【讨论】: