项目中需要使用base4,克隆了一个下来https://github.com/dankogai/j...
因为小程序目前不支持直接引入 node_modules 所以我写的是路径
我在app.js里根据路径写
require('./node_module/js-base64/base64.js');
但是报错
WAService.js:3 TypeError: Cannot read property 'Base64' of undefined
因为源码里面并没有exports Base64对象出来,自己手动exports就可以了
目前来说,我也没找到好的方法,只能修改源码强制导出
小程序的运行时环境既不是Node.js也不是浏览器,也没有暴露全局对象。。。
在Base64源码的最开始
global = {}; //在最开始加上这个 var _Base64 = global.Base64;
在最后
// that's it! if (global['Meteor']) { Base64 = global.Base64; // for normal export in Meteor.js } module.exports = global.Base64; //最后加上这个
这样就能用了
import base64 from './node_module/js-base64/base64.js'
最近也刚上手小程序,有问题欢迎一起交流
微信小程序不知道node modules 所以要用的话你只能把这段代码复制粘贴到你的小程序js中,官方文档有写。