【发布时间】:2017-08-20 06:16:24
【问题描述】:
我只是尝试使用fs.readFileSync 读取文件,但似乎找不到。
我确保声明了它,并在我的构造函数中添加了它:
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。有没有一种特殊的方法可以在 Typescript 中包含 fs ?
【问题讨论】:
-
你的
Login组件是浏览器组件,对吧?你为什么要使用fs模块? -
如果您使用的是 wepback,您可以使用
raw-loader获取文件内容。
标签: node.js typescript