how to use a json file in a typescript file
I have a file called men.json.
I want to do the equivalent of var men = require('./men.json');
.
Whatever I have tried it looks for ./men.json.js
file.
I read that I can not use import
since it is not a ts
file.
What is the equivalent required line?
declare module '*.json' {
var _: any;
export default _;
}
then you can import mem from './mem.josn'
put this into some file that's included in tsconfig.json
now you can require .json
, this works for any other file formats (though you need webpack since nodejs cannot require them directly)
You should try requiring as follows:
// foo.ts
var mem = require('./mem.json);
console.log(mem);
Then compile ...
tsc foo.ts
This will give a generic typescript error, like:
error TS2304: Cannot find name 'require
which you could ignore ... or preferably, get rid of by installing the necessary typings - or package if you are using typescript 2 or later. (see typescript getting error TS2304: cannot find name ' require')
链接地址: http://www.djcxy.com/p/47506.html上一篇: jQuery ajax调用重定向到另一个域的域(未遵循)
下一篇: 如何在打字稿文件中使用json文件