简体   繁体   English

如何在Angular应用程序中导入npm模块?

[英]How to import a npm module in Angular Application?

I came across this npm package which groups array according to our requirement. 我遇到了这个npm包,该包根据我们的需求对数组进行分组。 I need to use this package in my angular application to group my array. 我需要在我的角度应用程序中使用此包来对数组进行分组。 How should I import these package to my angular application. 我应该如何将这些包导入我的角度应用程序。 Is there are any other module present to group array. 是否有其他模块可以对数组进行分组。 https://www.npmjs.com/package/group-array#examples https://www.npmjs.com/package/group-array#examples

You should use import it using ES6 syntax example: 您应该使用ES6语法示例导入它:

import groupArray from 'group-array';

Here is stackblitz demo 这是stackblitz演示

This is exactly the syntax used to import angular modules 这正是导入角度模块的语法

import { Observable} from 'rxjs' 从'rxjs'导入{Observable}

as type follow syntex of ES6 import will work here 作为类型跟随ES6导入的syntex将在这里工作

If you are using typescript then 如果您使用打字稿,则

  import * as ga from "../../../../../node_modules/group-array"; //proper path to your node_modules/group-array

  arr = [
    {tag: 'one', content: 'A'},
    {tag: 'one', content: 'B'},
    {tag: 'two', content: 'C'},
    {tag: 'two', content: 'D'},
    {tag: 'three', content: 'E'},
    {tag: 'three', content: 'F'}
  ];  

  ngOnInit() {
    console.log(ga(this.arr, 'tag'));
  }

Can you plz try this once, install it first using npm 您能否尝试一次,请先使用npm安装

npm install group-array

import * as groupArray from 'group-array'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM