如何在angular4中导入名称中带有连字符的npm模块

时间:2018-09-21 01:43:27

标签: node.js angular

我想在angular4项目中使用array-to-tree npm模块。但是,当我使用import { array-to-tree } from 'array-to-tree';时,它会给出错误期望的字符串葬礼

请帮助。

2 个答案:

答案 0 :(得分:2)

我猜您可以使用:

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

然后arrayToTree是您可以使用的功能。

答案 1 :(得分:0)

请尝试这个

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 );
  }
}

注意:如果不将arrayToTree(this.dataOne)的结果分配给变量,则打字稿将引发错误

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