嘿我正在使用react-app和我做一个小项目 我一直在努力为this module创建导出。
我已经用npm安装了它,我想编辑它以便我可以导入并在我的app.js中使用它
我尝试使用class \ function \ let定义“reddit”并使用:
export default
module.exports
和
import reddit from 'reddit.js';
var reddit = require('reddit.js');
尝试使用模块中的简单函数进行检查:
console.log(reddit.hot('cats'));
但我还是得到了:
Uncaught TypeError: reddit.hot is not a function
我有点迷茫,我做错了什么?
答案 0 :(得分:1)
该模块使用window.reddit全局变量。不要覆盖它! 因此,如果您在客户端,请使用:
require('reddit.js')
//...
reddit.hot('cats')
对于服务器端 - 你必须做一些技巧才能使它工作,因为在服务器端你没有'window'全局变量。
UPD:
后端使用示例:
const window = {};
const r = require('./reddit.js') // You don't really use r
const reddit = window.reddit;
reddit.hot('cats')
答案 1 :(得分:0)
Reddit不会导出任何内容,因为它主要设计为添加为脚本标记!
// So for CommonJS module:
require('reddit.js');
//And for ES6 modules
import 'reddit.js';
您将能够通过窗口对象访问reddit和reddit.hot()方法。