How to import npm module having hyphen in name in angular4

Viewed 1827

I want to use array-to-tree npm module in angular4 project. But when I use import { array-to-tree } from 'array-to-tree'; it gives error string leteral expected.

Please help.

2 Answers

At a guess I’d say you can use:

import * as arrayToTree from ‘array-to-tree’

and then arrayToTree is the function you can use.

Please try this

import * as arrayToTree from 'array-to-tree';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  dataOne = [
    {
      id: 1,
      name: 'Portfolio',
      parent_id: undefined
    },
    {
      id: 2,
      name: 'Web Development',
      parent_id: 1
    },
    {
      id: 3,
      name: 'Recent Works',
      parent_id: 2
    },
    {
      id: 4,
      name: 'About Me',
      parent_id: undefined
    }
  ];

  data = arrayToTree(this.dataOne);

  constructor() {
    console.log(this.data );
  }
}

Note: If you don't assign result of arrayToTree(this.dataOne) to a variable then typescript will throw error

[ts] Function implementation is missing or not immediately following the declaration.
(method) AppComponent.arrayToTree(this: any, dataOne: any): any
Related