[英]Trying to NPM publish and install a custom angular2 component with Angular-CLI; only compiles the first time
I'm having a most unusual and frustrating problem. 我遇到了一个最不寻常且令人沮丧的问题。
I have an Ng2 component called via-date-picker that I am trying to NPM publish so that it can be easily used in other projects. 我正在尝试NPM发布Ng2组件,称为via-date- picker,以便可以在其他项目中轻松使用。 In order to do so, I have made it into an Angular2 component library.
为了做到这一点,我将其放入Angular2组件库。 The via-date-picker exports a module called ViaDatePickerModule , which I want to import elsewhere.
via-date- picker导出了一个名为ViaDatePickerModule的模块,我想将其导入其他地方。
In order to test and make sure that it is being published correctly, I am NPM-installing and importing it into an otherwise empty Angular-CLI project that I am calling npm-test . 为了测试并确保它被正确发布,我正在安装NPM并将其导入另一个我叫npm-test的空Angular-CLI项目中。
So I run my npm-test application using "ng serve", and I get this error: 所以我使用“ ng serve”运行我的npm-test应用程序,并且出现此错误:
ERROR in ViaDatePickerModule is not an NgModule webpack: Failed to compile
ViaDatePickerModule中的错误不是NgModule Webpack:无法编译
Yet despite that error, the project compiles anyway: 尽管存在该错误,该项目还是会编译:
And when I open up my project, lo and behold, everything works!! 当我打开项目时,瞧瞧,一切正常!!
But this only happens the first time that I run the project. 但这仅在我第一次运行项目时发生。 On successive attempts to run the project via "ng serve", I get the same compilation error, but this time the project just flat-out refuses to complete it's compilation:
通过“ ng serve”连续尝试运行项目时,我遇到了相同的编译错误,但是这次只是平淡无奇的项目拒绝完成其编译:
I have no idea why I'm getting this error, and why Angular-CLI will run my project sometimes but not others. 我不知道为什么会收到此错误,为什么Angular-CLI有时会运行我的项目,而其他人不会运行。
I've scoured the web for answers and tried every solution I can find for this error, as well as every other thing I can think of: 我在网上搜寻了答案,并尝试了可以找到的解决此错误的所有方法,以及我能想到的所有其他方法:
No matter what I do, I keep coming to the same webpack error that I posted above; 无论我做什么,我都会遇到上面发布的同一webpack错误; that ViaDatePickerModule is not an NgModule.
ViaDatePickerModule不是NgModule。 I'm completely out of ideas.
我完全没有主意。 Any help that anyone could provide would be crazy helpful.
任何人都可以提供的任何帮助都是疯狂的帮助。
For the sake of complete thoroughness, I've created a public repo on github here containing all the files involves, divided into two main directories: 为了完整起见,我在github上创建了一个公共仓库,其中包含所有涉及的文件,分为两个主要目录:
Again, any help that anyone could provide would be extremely, extremely appreciated! 同样,任何人都可以提供的任何帮助将非常非常感谢! Really, I would be eternally grateful.
真的,我将永远感激不已。
If you are %100 sure that ALL of your consumers will import your components, modules ...etc from a TS
project such as angular-cli
. 如果您100%确定所有消费者都将从
TS
项目(例如angular-cli
导入组件,模块... angular-cli
You can publish your TS
source directly without transpiling. 您可以直接发布
TS
源,而无需进行编译。 Ie. 就是 you'll be publishing static
.ts
files that can be imported in any project that will do the transpiling for you. 您将发布静态
.ts
文件,这些文件可以导入将为您进行转码的任何项目中。
However, if your want your library to also be consumed as a JS
es5
or es6
module , then you should transpile. 但是,如果您还希望将库作为
JS
es5
或es6
模块使用 ,则应该进行转换。
Also, you can try the angular compiler ngc
instead of the typescript compiler tsc
? 另外,您可以尝试使用角度编译器
ngc
而不是打字稿编译器tsc
? ngc
is a wrapper around tsc
. ngc
是tsc
的包装器。 You could start there, There are many library starters put there that can help you start an angular library and get it optimized for AOT
compilation. 您可以从这里开始。有很多库启动器可以帮助您启动角度库并使它针对
AOT
编译进行优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.