我需要使用webpack从打字稿中导入原始数据。这是我的设置:
$ tree
.
+-- file.txt
+-- main.ts
+-- package.json
+-- tsconfig.json
+-- webpack.config.js
file.txt
file-content
main.js
import file from './file.txt';
console.log(file);
package.json
{
"devDependencies": {
"raw-loader": "^0.5.1",
"ts-loader": "^3.1.1",
"typescript": "^2.6.1",
"webpack": "^3.8.1"
}
}
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"baseUrl": "app"
},
"exclude": [
"node_modules"
]
}
webpack.config.js
const path = require('path');
const config = {
entry: './main.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.js'
},
module: {
rules: [
{ test: /\.txt$/, use: 'raw-loader' },
{ test: /\.tsx?$/, loader: 'ts-loader' },
]
}
};
module.exports = config;
当我运行weback时,它说找不到模块:
ERROR in ./main.ts
[tsl] ERROR in /tmp/test/main.ts(1,18)
TS2307: Cannot find module './file.txt'.
我的问题是:如何将txt数据导入打字稿文件?例如,在编写角度组件并导入html模板以将其分配给该组件的template属性时,这很有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句