我只是想用fs.readFileSync
来读取文件,虽然看起来不能找到它。在打字稿中使用fs
我确信声明一下,我的构造函数中添加它:
export default class Login extends React.Component<LoginProps, {}> {
private webAuth: auth0.WebAuth;
fs: any;
constructor(props: any, context: any) {
super(props, context);
this.fs = require('fs');
this.webAuth = new auth0.WebAuth({
clientID: conf.auth0.clientId,
domain: conf.auth0.domain,
responseType: 'token id_token',
redirectUri: `${window.location.origin}/login`
});
}
[...]
,并用它在一个简单的函数:
verifyToken = (token) => {
console.log(this.fs);
let contents = this.fs.readFileSync('../utils/public.key', 'utf8');
console.log(contents);
}
但是,这提出了一个Uncaught TypeError: _this.fs.readFileSync is not a function
。在打字稿中是否包含fs
的特殊方法?
这个你的'Login'组件是一个浏览器组件,对吧?你为什么试图使用'fs'模块? – Diullei
如果您使用wepback,您可以使用'raw-loader'来请求文件内容。 –