发现微信小程序开发时无法正常引入第三方JS库,分析了后发现,微信小程序无法正常执行类似如下js代码:
( function (self) { 'use strict' ; console.log( '*** self in fetch.js: ' , self) if (self.fetch) { return } })( this ); |
出错信息如下:
at index.js? [sm]:6 at index.js? [sm]:3 at require (WAService.js:9) at appservice:55 |
显然,并没有把 this 赋值给 self
不知要如何处理,才能让第三方JS库中有类似如上代码的库能正常引入到微信小程序的JS文件代码中。
对接浙里办微信小程序引入浙里办提供的js库 ,这个js库初始化一直不成功,该如何解决 //assets.zjzwfw.gov.cn/assets/ZWJSBridge/1.1.0/zwjsbridge.js
谢谢了,我实际的问题是引入MongoDB-stitch-client出现问题,看看大侠是否有兴趣帮我看看其源代码怎么改下就能用了?
最小化了stitch JS库文件下载:https://s3.amazonaws.com/stitch-sdks/js/library/stable/stitch.min.js
没有最小化的stitch JS库文件下载:https://s3.amazonaws.com/stitch-sdks/js/library/stable/stitch.js
抱歉他这方法有问题,我刚自己试了下,你把
var
root =
this
;
var
previousUnderscore = root._; 这两句注释掉就行了
然后
var _ = require('../../underscore/underscore.js');
var obj = {name: 'wyuye', age: 18};
console.log(_.keys(obj));
试了个方法可以用
我试下Underscore.js 1.8.3,“小程序引入underscore.js的解决方案 http://www.jb51.net/article/93601.htm”
问题无法解决,还不到方案上所说的代码行即出错:
// Underscore.js 1.8.3
// http://underscorejs.org
// (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
(
function
() {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `exports` on the server.
var
root =
this
;
// Save the previous value of the `_` variable.
var
previousUnderscore = root._;
因为没有this,所以上面最后一行会出错:
thirdScriptError
Cannot read property '_' of undefined;at pages/index page lifeCycleMethod onLoad function
TypeError: Cannot read property '_' of undefined
at http://127.0.0.1:9973/appservice/utils/underscore.js:19:32
这里有小程序引入underscore.js的解决方案 http://www.jb51.net/article/93601.htm
真的要哭了。
微信小程序框架为什么不提供全局对象呢?大家如何处理我这类问题呢?
没有全局对象
self和window都是undefined
我发现很多第三方的JS库都无法引入,像Underscore.js,求大侠们指点:
如引入window.fetch polyfill就会出现如下的问题,就是上面大侠说的没有全局对象导致:
var
fetch = require(
'../utils/fetch.js'
);
WAService.js:11 Uncaught TypeError: Cannot read property
'fetch'
of undefined
at fetch.js? [sm]:4
at fetch.js? [sm]:1
at require (WAService.js:11)
at WAService.js:11
at index.js? [sm]:6
at require (WAService.js:11)
at appservice:55
/** */
console.log(
'### this: '
,
this
);
(
function
(a) {
'use strict'
;
console.log(
'*** self in fetch.js: '
, a)
if
(a.fetch) {
return
}
})(
this
);
/**/
还真是这样子:
### this: undefined
index.js? [sm]:7 *** self in fetch.js: undefined
index.js? [sm]:8 Uncaught TypeError: Cannot read property 'fetch' of undefined
at index.js? [sm]:8
at index.js? [sm]:5
at require (WAService.js:11)
at appservice:55