我想在角度打字稿中使用数组到树的npm模块。我尝试了import * as arraytotree from 'array-to-tree';
,并尝试使用构造函数console.log(arraytotree(this.dataOne));
。
但是我在控制台中遇到错误 ERROR 错误:arraytotree不是函数
请帮助
答案 0 :(得分: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 );
}
}
package.json
"array-to-tree": "^3.3.0"
注意::如果不将arrayToTree(this.dataOne)的结果分配给变量,则打字稿将引发错误
[ts] Function implementation is missing or not immediately following the declaration.
(method) AppComponent.arrayToTree(this: any, dataOne: any): any