Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using lodash.clonedeep in Angular 5

First, I have never used lodash.clonedeep before but know JavaScript fairly well.

From my package,json file: "lodash.clonedeep": "4.5.0",

import { cloneDeep } from 'lodash.clonedeep';

editStart(): void {
   this.oldData = cloneDeep(this.currentData);
   this.editing = true;
}

Error: ERROR TypeError: lodash_clonedeep_1.cloneDeep is not a function

Help very MUCH appreciated since I'm out of options, have read and tried a lot of options. I have a workaround, using several objects, but want to avoid if possible.

like image 312
SveinS Avatar asked Feb 25 '18 10:02

SveinS


People also ask

What is the use of Lodash cloneDeep?

Lodash is an excellent JavaScript utility library for those not knowing it yet. The cloneDeep method will iterate all levels of the original Object and recursively copying all properties found.

Should I use Lodash in angular?

Adding Lodash To AngularWhat we also need is the type definitions to give us some nice strongly typed defintions inside Typescript. For that we need to install one more package. Note that we only add the type definitions as a dev dependency as it is not required at runtime, only while you are developing your project.

What is the use of Lodash in angular?

Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc. Lodash's modular methods are great for: Iterating arrays, objects, & strings. Manipulating & testing values.

What is JavaScript cloneDeep?

A deep copy is a copy of all elements of the original object. Changes made to the original object will not be reflected in the copy. In this article, you will create deep copies of objects using the Lodash library.


1 Answers

Your import statement is incorrect.

You should either import the full library:

import * as _ from 'lodash';
...
let foo = _.cloneDeep(bar);

Or import just the cloneDeep function:

import * as cloneDeep from 'lodash/cloneDeep';
...
let foo = cloneDeep(bar);
like image 185
agascon Avatar answered Sep 30 '22 19:09

agascon