0
.json 파일을 비동기 적으로로드하려고합니다. .js 파일에 대한 예제가 있지만 typescript를 사용하고 있으며 솔루션을 찾지 못하는 것 같습니다..json 파일을 webpack과 비동기 적으로로드하십시오.
webpack.config.js
var webpack = require('webpack');
module.exports = {
entry: "./src/app.ts",
output: {
path: './dist',
filename: "bundle.js"
},
resolve: {
extensions: ['', '.ts', '.tsx', '.js', '.jsx', '.json']
},
module: {
loaders: [
{ test: /\.css$/, loader: "style!css" },
{ test: /\.ts$/, loader: 'ts-loader'},
{ test: /\.json$/, loader: 'json-loader'},
{
test: /jquery\.min\.js$/,
loader: 'expose?jQuery'
},
{
test: /jquery\.min\.js$/,
loader: 'expose?$'
}
]
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
'window.$': 'jquery'
})
]
};
app.ts
는 콘솔에 오류가 있습니다declare var require: {
<T>(path: string): T;
(paths: string[], callback: (...modules: any[]) => void): void;
ensure: (paths: string[], callback: (require: <T>(path: string) => T) => void) => void;
};
require(['./assets/data.json'], function(data) {
console.log(data); //doesn't log anything.
});
,
GET http://localhost:5557/1.bundle.js 404 (Not Found)
그러나, 나는 비동기 적으로 시도하지 않는 경우, 잘 작동합니다.
console.log(require('./assets/data.json')); // logs the json just fine
감사합니다.
내 설정의 출력에 publicPath : './dist'를 추가했지만 여전히 오류가 변경되지 않았습니다. – Anik
'./dist'를 넣지 말고'/ dist'를 넣으십시오. publicPath는 브라우저에서 사용됩니다. – Ambroos
'/ dist'를'localhost : 5557/dist1.bundle.js'라고 입력하면 값을'/ dist /'로 바꾼 다음'localhost : 5557/dist/1.bundle.js' 존재합니다. 답변 주셔서 감사합니다 :) – Anik