小程序使用 lodash 的问题
import _ from 'lodash'
报错:
vendor.js:11874 Uncaught TypeError: Cannot read property 'prototype' of undefined
at runInContext (vendor.js:11874)
at Object.<anonymous> (vendor.js:27538)
at Object.<anonymous> (vendor.js:27566)
at Object.<anonymous> (vendor.js:27568)
at __webpack_require__ (manifest.js:59)
at Object._ (app.js? [sm]:15)
at __webpack_require__ (manifest.js:59)
at Object.webpackJsonpCallback [as webpackJsonpMpvue] (manifest.js:30)
at app.js? [sm]:3
at require (WAService.js:1)
报错分析见 这里
如果不想修改 lodash.js 文件的话,只需要在 import 'lodash'
之前先执行下面的代码:
global.Object = Object
global.Array = Array
global.Buffer = Buffer
global.DataView = DataView
global.Date = Date
global.Error = Error
global.Float32Array = Float32Array
global.Float64Array = Float64Array
global.Function = Function
global.Int8Array = Int8Array
global.Int16Array = Int16Array
global.Int32Array = Int32Array
global.Map = Map
global.Math = Math
global.Promise = Promise
global.RegExp = RegExp
global.Set = Set
global.String = String
global.Symbol = Symbol
global.TypeError = TypeError
global.Uint8Array = Uint8Array
global.Uint8ClampedArray = Uint8ClampedArray
global.Uint16Array = Uint16Array
global.Uint32Array = Uint32Array
global.WeakMap = WeakMap
global.clearTimeout = clearTimeout
global.isFinite = isFinite
global.parseInt = parseInt
global.setTimeout = setTimeout
你可以把这段代码写到某个文件,比如 utils/index.js, 只要先 import 'utils'
, 然后再 import 'lodash'
就 OK
+V why_null 请备注:from博客园