从node_module导入一个类并在Angular 6中使用它会引发错误

时间:2018-06-14 01:19:17

标签: angular

这是我的方案

app.component.ts

import { Component } from '@angular/core';
import { Nut } from 'nuts';

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

  constructor(){
    console.log(new Nut());
  }
}

node_modules / nuts(本地安装)

为了能够在本地安装,我必须在"preserveSymlinks": true中添加到angular.json中的构建选项。

node_modules /螺母/ index.ts

export class Nut {
    public size: number;
}

这是ng serve

上的错误
ERROR in ./node_modules/nuts/index.ts
Module build failed: Error: /Users/charliewalter/Desktop/my-project/peanuts/node_modules/nuts/index.tsis missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
The missing file seems to be part of a third party library. TS files in published libraries are often a sign of a badly packaged library. Please open an issue in the library repository to alert its author and ask them to package the library using the Angular Package Format (https://goo .gl/jB3GVv).
    at AngularCompilerPlugin.getCompiledFile (/Users/charliewalter/Desktop/my-project/peanuts/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:709:23)
    at plugin.done.then (/Users/charliewalter/Desktop/my-project/peanuts/node_modules/@ngtools/webpack/src/loader.js:41:31)
    at process._tickCallback (internal/process/next_tick.js:68:7)
ℹ 「wdm」: Failed to compile.

1 个答案:

答案 0 :(得分:0)

为Angular编译npm包,因为Angular不支持从public override bool OnCreateOptionsMenu(IMenu menu) { // return base.OnCreateOptionsMenu(menu); MenuInflater.Inflate(Resource.Menu.actionmenu, menu); // var dd = menu.FindItem(Resource.Id.icon_group); IMenuItem item = menu.FindItem(Resource.Id.ic_group); LayerDrawable icon = item.Icon as LayerDrawable; // LayerDrawable icon = (LayerDrawable)item.Icon; CountDrawable badge; Drawable reuse = icon.FindDrawableByLayerId(Resource.Id.ic_group_count); if (reuse != null && reuse is CountDrawable) { badge = (CountDrawable)reuse; } else { badge = new CountDrawable(this); } badge.setCount("8"); badge.GetBounds=icon.Bounds; icon.Mutate(); icon.SetDrawableByLayerId(Resource.Id.ic_group_count, badge); return true; } 目录之外构建。

请参阅: https://github.com/cjonasw/nuts/tree/master