Vuejs2 在vue文件中导入lodash时出错

Vuejs2 在vue文件中导入lodash时出错,vuejs2,lodash,Vuejs2,Lodash,在我的@vue/cli 4.0.5应用程序中,我正在运行后尝试导入lodash库 npm i lodash 当我尝试调用isEmpty方法时,出现以下错误: Property or method "isEmpty" is not defined on the instance but referenced during render. 以下是我在vue文件中导入它的方式: <template> <article class="event_item_container"

在我的@vue/cli 4.0.5应用程序中,我正在运行后尝试导入lodash库
npm i lodash

当我尝试调用
isEmpty
方法时,出现以下错误:

Property or method "isEmpty" is not defined on the instance but referenced during render.
以下是我在vue文件中导入它的方式:

<template>
    <article class="event_item_container">

        <span v-if="!isEmpty(example)">{{example}}</span>

        ...
    </article>
</template>

<script>
    import {bus} from '../../main'
    import axios from 'axios'

    import Vue from 'vue'

    import * as lodash from 'lodash'
    // Vue.use(lodash) // I tried uncomment this line the same error

    // import isEmpty from "lodash/isEmpty" // Aso I tried to uncomment this line, the same error

{{example}}
...
从“../../main”导入{bus}
从“axios”导入axios
从“Vue”导入Vue
从“lodash”导入*作为lodash
//use(lodash)//我试图取消注释这一行,但出现了相同的错误
//import isEmpty from“lodash/isEmpty”//Aso我试图取消这一行的注释,同样的错误
我试图导入到src/main.js文件-同样的错误


哪种方法有效?

有几件事你可以试试

  • 尝试添加
  • 而不是将*从“lodash”导入为lodash。然后尝试使用
    。.isEmpty

  • 您可以尝试导入
    isEmpty
    like
  • 您可以尝试卸载lodash(从
    package.json
    中删除),然后运行
    npm i--save lodash
  • 编辑


    从NPM5开始,
    npm install
    npm install--save
    之间没有区别,因为
    npm install
    现在将包添加到
    devDependencies
    ;它在NPM5之前没有做过的事情(这就是为什么使用了
    npm安装--save

    因为您将*作为lodash从'lodash'导入,,所以您必须像
    一样使用它!lodash.isEmpty(示例)
    而不是
    !I空(示例)

    谢谢!请你澄清第3点好吗?我必须用命令npm I--save lodash重新安装软件包吗?@PetroGromovo I实际上刚刚做了一些研究,结果表明,由于NPM5
    npm install
    完成了npm install--save所做的事情(将软件包添加到package.json中的开发依赖项中)。如果您使用的是NPM5+
    npm,那么我就可以了。如果您使用的是较低的版本,请继续使用
    npm安装--save
    。卸载和重新安装仍然没有什么坏处,但如果您使用的是npm5,就不必使用
    --save
    +
    import lodash from 'lodash'; 
    Vue.prototype._ = lodash
    
    import { isEmpty } from 'lodash';