【问题标题】:How to make lodash import in JSBin?如何在 JSBin 中进行 lodash 导入?
【发布时间】:2019-04-18 19:33:27
【问题描述】:

我需要在 JSBin 中用 lodash 显示一段代码,但无法弄清楚如何进行“获取”导入。我有"ReferenceError: require is not defined https://jsbin.com/hotobir/edit?js,console

// Normally that is working fine
import { get } from 'lodash'

//No import get from 'lodash/get'
//No const { get } = lodash

const obj = { myKey: 'myValue' }
console.log(get(obj, 'myKey', 'notThere'))

如何进行此导入?

【问题讨论】:

    标签: javascript ecmascript-6 lodash jsbin


    【解决方案1】:

    是的,Add Library 选项也没有按我预期的方式工作。

    它将脚本标记添加到 html 输出中,因此如果您不进行任何更改,它将不起作用。

    解决方案:只需将script标签移到head标签中即可。

    【讨论】:

      【解决方案2】:

      默认情况下,JSBin 将脚本导入放置在错误的位置。它应该在html的头部。

      const {get} = _
      const obj = { myKey: 'myValue' }
      console.log(get(obj, 'myKey', 'notThere'))
      

      【讨论】:

        【解决方案3】:

        要在 jsbin 中添加任何库,请将光标放在 html 的头部,添加库,并确保在 <script 标记中添加 language='javascript'。这种方式至少对我有用。

        例如。从此:

          <script
            src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"> 
          </script>
        

        到这里:

          <script
            language='javascript'
            src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"> 
          </script>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-02-02
          • 1970-01-01
          • 1970-01-01
          • 2021-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-24
          相关资源
          最近更新 更多