簡體   English   中英

如何在angular4中導入名稱中帶有連字符的npm模塊

[英]How to import npm module having hyphen in name in angular4

我想在angular4項目中使用array-to-tree npm模塊。 但是,當我import { array-to-tree } from 'array-to-tree';使用import { array-to-tree } from 'array-to-tree'; 它給出錯誤字符串leteral預期

請幫忙。

我猜你可以使用:

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

然后arrayToTree是您可以使用的函數。

請嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM