javascript - 由于 lodash 在 Vue 中使用 Jest 进行测试在子组件中失败
问题描述
在我的 Vue 应用程序中,lodash 作为 Vue 插件全局加载main.js
:
import _ from "lodash"
export default function install(Vue) {
Vue.prototype._ = _
Vue.mixin({
computed: {
_: () => _
}
})
}
然后,我有一个使用 lodash 函数的子组件,我正在尝试shallowMount
它的父组件,但是当我运行测试时它失败并出现以下错误:
ReferenceError: _ is not defined
270 | },
> 272 | debouncedSearch: _.debounce(
| ^
273 | function (value) {
274 | this.currentQuery = value
275 | if (!_.isEmpty(this.currentQuery)) {
我在这里缺少什么?
解决方案:
在查看标记为正确的 awnser 之后,我添加import debounce from "lodash/debounce"
并替换_.debounce()
了debounce
它,它工作正常。
解决方案
尝试像这样重写你的函数:
// Lodash is not defined out here
debouncedSearch: function() {
// But is available in here
return _.debounce(
...
);
}
推荐阅读
- gitlab - 如何为 Gitlab 指定 pre-commit --from-ref?
- android - react-native run-android:此版本中使用了已弃用的 Gradle 功能,使其与 Gradle 7.0 不兼容
- security - Wildfly 24,部署不使用wildfly安全的应用
- javascript - 我想动态使用 ngb 字体,但是当我点击任何一个时,它会打开所有的字体
- asynchronous - 什么是异步,响应式编程必须是异步的吗?
- android - 我无法在 gradle 7.0.0 和 android studio 2020.3.1 中添加 MPAndroidChart
- android - 如何解决“Attribute android: ** not allowed here”错误?
- reactjs - 将认知用户名显示为文本
- python-3.x - 正则表达式第一次出现字符串搜索给出 NULL 输出
- unix - awk 获取一年中所有月份的最后一个工作日