简体   繁体   English

无法确定组件角度 5 的模块

[英]cannot determine the module for component angular 5

I'm getting following error when I build the Angular app using "ng build --prod".使用“ng build --prod”构建 Angular 应用程序时出现以下错误。 This is working when I build with Angular 4 and getting error with Angular 5. With Angular 5 "ng serve" is working perfectly.当我使用 Angular 4 构建并使用 Angular 5 出错时,这是有效的。使用 Angular 5,“ng serve”运行良好。

ERROR in Error: Cannot determine the module for class TimeAgoPipe in mypath/node_modules/time-ago-pipe/time-ago-pipe.ts!错误中的错误:无法确定 mypath/node_modules/time-ago-pipe/time-ago-pipe.ts 中 TimeAgoPipe 类的模块! Add TimeAgoPipe to the NgModule to fix it.将 TimeAgoPipe 添加到 NgModule 以修复它。

Getting error for https://www.npmjs.com/package/time-ago-pipe https://www.npmjs.com/package/time-ago-pipe出现错误

Any solutions ?有什么解决办法吗?

Check Case Sensitivity检查区分大小写

import { MyComponent } from "./User/my-component";

In my case, the problem was that the folder user was created with U as capital in my import statement.就我而言,问题是文件夹用户是在我的导入语句中以 U 为大写创建的。 But actually on disk it was in small case.但实际上在磁盘上它是小情况。 When I renamed the folder to user (with u lower case) in my import statement also, it worked for me.当我在导入语句中将文件夹重命名为 user (用小写字母)时,它对我有用。

import { MyComponent } from "./user/my-component";

So check the case sensitivity, of your import path.因此,请检查导入路径的区分大小写。

Angular 5 and specifically angular cli 1.5 has default ahead of time compilation turned on and it needs to know module for all components/pipes etc. that it finds in your project folder if you have some components that aren't declared in any module it will throw errors. Angular 5,特别是 angular cli 1.5 的默认提前编译打开,它需要知道所有组件/管道等的模块,如果您有一些未在任何模块中声明的组件,它会在您的项目文件夹中找到抛出错误。

You can either declare TimeAgoPipe in some module:您可以在某个模块中声明TimeAgoPipe

@NgModule({
  declarations: [TimeAgoPipe, ...]
})
export class AppModule {}// for example can be any other used module

or try running build without ahead of time compilation resulting code will work slower或者尝试在没有提前编译的情况下运行构建,结果代码会运行得更慢

ng build --prod --aot=false

third way if you don't use that pipe at all you can add it to excluded files in tsconfig.json第三种方法,如果您根本不使用该管道,则可以将其添加到tsconfig.json排除文件中

{
  ...
  "exclude": [ "path/to/unused/component.ts", ... ]
}

In my case, the error was because of multiple definitions of the same class in various files.在我的情况下,错误是因为不同文件中同一类的多个定义。 This happened due to copy-paste codes by some developer.这是由于某些开发人员复制粘贴代码而发生的。

I had exactly the same issue and can offer a workaround although not sure why this specific pipe is not being successfully picked up by the aot build:我遇到了完全相同的问题,可以提供一种解决方法,尽管不确定为什么 aot 构建没有成功获取此特定管道:

Workaround解决方法

Copy the time-ago-pipe.ts from node_modules/time-ago-pipe and just copy this file into your own project.从 node_modules/time-ago-pipe 复制 time-ago-pipe.ts 并将此文件复制到您自己的项目中。

Add it to your declarations in your module as normal and import this into your imports eg:像往常一样将它添加到您的模块中的声明中,并将其导入到您的导入中,例如:

import { TimeAgoPipe } from './_pipes/time-ago-pipe';

This will then compile successfully in AOT build and you can still use the pipe.这将在 AOT 构建中成功编译,您仍然可以使用管道。

Ran into this issue and wanted to consolidate existing suggestions, and suggest some general strategies when nothing else worked for me:遇到这个问题并想整合现有的建议,并在没有其他方法对我有用的情况下提出一些一般策略:

  1. In my case, the error was it could not determine the module for a Pipe ;就我而言,错误是无法确定Pipe的模块; I was using custom Pipe injected straight into an Angular Service .正在使用直接注入 Angular Service自定义Pipe
  • When I stopped using the Pipe , the issue went away;当我停止使用Pipe ,问题就消失了; so I removed the Pipe transform -- I moved the transformation to a different Service , stopped depending on Pipe altogether.所以我删除了Pipe转换——我将转换移到了不同​​的Service ,完全取决于Pipe停止。
  1. Make sure you declared (and exported), your Pipe/Component properly in the declarations (and exports ) properties of your NgModule确保在NgModuledeclarations (和exports )属性中正确声明(和导出)管道/组件
  2. If it is a 3rd party Pipe/Component (ie inside node_modules ), copy the Pipe to your own project (if possible), and/or re-export the Pipe from one of your NgModule如果它是第 3 方管道/组件(即在node_modules ),请将管道复制到您自己的项目(如果可能),和/或从您的NgModule之一重新导出管道
  3. If you don't need the Pipe/Component, you can exclude the file by adding to the exclude property of the tsconfig.json (this may not be an option for everyone)如果您不需要管道/组件,您可以通过添加到tsconfig.jsonexclude属性来排除该文件(这可能不是每个人的选择)
  4. Or build without ahead-of-time compilation ng build --aot=false (may be a faster build, but then creates a non-optimized app; learn more about AOT)或者在没有提前编译的情况下ng build --aot=false (可能是更快的构建,但随后会创建一个未优化的应用程序;了解有关 AOT 的更多信息)
  5. If it is your Pipe/Component (vs 3rd party), confirm you don't double-define your Pipe class, and don't double-declare your Pipe: ie add to the declaratations array of two or more different NgModule s如果它是您的管道/组件(与第 3 方),请确认您没有双重定义您的管道类,也不要双重声明您的管道:即添加到两个或多个不同NgModuledeclaratations数组中
  6. In your Angular typescript files where you import { YourPipe } from './yoUr-pippe.ts' , make sure there are no typos or capitalization issues in your import statement, especially the filenameimport { YourPipe } from './yoUr-pippe.ts'的 Angular 打字稿文件中,确保导入语句中没有拼写错误或大小写问题尤其是文件名
  7. In my case, the above didn't fix my problems.就我而言,上述内容并没有解决我的问题。 I did many of the steps below, eventually the problem stopped.我做了下面的许多步骤,最终问题停止了。 I'm not sure which one did the trick:我不确定哪一个成功了:
    • Ensure any ts file which uses the problematic Pipe/Component, lives in an NgModule which import s the NgModule that defined the Pipe/Component !确保任何使用有问题的管道/组件的ts文件位于一个NgModule ,该NgModule import了定义管道/组件的NgModule

      • For example if AModule , defines AComponent , which uses BPipe , defined in BModule , then AModule must import the BModule例如,如果AModule定义了AComponent ,它使用BPipe ,在BModule定义,那么AModule必须import BModule
        • In my case, I re-arranged my NgModule dependency tree.就我而言,我重新安排了我的NgModule依赖树。 I used a "shared module" pattern so my Pipe could be re-used in multiple places.我使用了“共享模块”模式,因此我的 Pipe 可以在多个地方重复使用。
    • Ensure there are no circular NgModule dependencies , like in these examples确保没有循环的NgModule依赖项,就像在这些示例中一样

    • If you are able, Upgrade NodeJS itself and/or angular and its tools ( angular-cli , etc)如果可以,请升级NodeJS本身和/或angular及其工具( angular-cli等)

      • I ran into unrelated issues, so I found this list of suggestions ... but these suggestions fixed other issues , and let me re-approach the Cannot determine the module... issue (which stopped happening!)我遇到了不相关的问题,所以我找到了这个建议列表......但这些建议解决了其他问题,让我重新解决Cannot determine the module...问题(停止发生!)
    • Be careful about using index.ts files aka "barrel files", and the order of exports小心使用index.ts文件又名“桶文件”,以及exports的顺序

    • A general problem-solving approach : Remove / comment-out any references to the problematic file ,一般解决问题的方法:删除/注释掉对有问题文件的任何引用

      • Remove the references one-at-a-time, from each file, until your angular app build works.从每个文件中一次删除一个引用,直到您的 angular 应用程序构建工作。 If removing from a specific file solves the problem, you know which file is the source of the problem如果从特定文件中删除解决了问题,您就知道哪个文件是问题的根源
      • If you remove the references from all files (which may be too hard to do; for me it was not too many/ not too hard);如果您从所有文件中删除引用(这可能太难了;对我来说不是太多/也不是太难); then your angular app should build...那么你的 Angular 应用程序应该构建......
      • Confirm your angular app still builds.确认您的 angular 应用程序仍在构建。 In my case, my angular build had unrelated errors which I could easily fix (for example, Can't bind to '___' since it isn't a known property of '___' , or Property '___' does not exist on type '___'. Did you mean '____' , or Expected 0 arguments, but got 1. )就我而言,我的 Angular 构建有一些无关的错误,我可以轻松修复(例如, Can't bind to '___' since it isn't a known property of '___' ,或者Property '___' does not exist on type '___'. Did you mean '____'还是Expected 0 arguments, but got 1.
      • After fixing any other problems, you can uncomment your code gradually, while ensuring your ng build works.修复任何其他问题后,您可以逐渐取消对代码的注释,同时确保您的ng build工作正常。 When I uncommented/added all my code back... my ng build no longer had the Cannot determine the module... issue当我取消注释/添加我的所有代码时...我的ng build不再有Cannot determine the module...问题

For me it didn't work even when component was there in declarations.对我来说,即使声明中有组件,它也不起作用。 So, I removed the component from declarations and from imports as well.因此,我也从声明和导入中删除了该组件。 And then I readded it by using Visual Studio code intellisense to auto complete component name in declarations and using auto import to import component at the top of the module file.然后我通过使用 Visual Studio 代码智能感知在声明中自动完成组件名称并使用自动导入在模块文件的顶部导入组件来阅读它。 And to my surprise it worked.令我惊讶的是它奏效了。

If any of the above solutions didn't work for you, then this is also worth trying.如果上述任何解决方案对您不起作用,那么这也值得一试。

  1. Import specific component.导入特定组件。
  2. Add component to @NgModule.将组件添加到@NgModule。

for me, the problem was in case sensitive in import paths.对我来说,问题是在导入路径中区分大小写。 but you must find all imports path that have problem.但是您必须找到所有有问题的导入路径。 not only import that used in your module.不仅导入在您的模块中使用的内容。

In my case, I had a pipe declared as their own class in mypipe.ts as well as in app.component.ts for some reason.就我而言,我有一个管道声明为自己的班级mypipe.ts以及在app.component.ts出于某种原因。 I remove the one in app.component and it compiled fine.我删除了 app.component 中的那个,它编译得很好。

In the error message, check where exactly it is complaining and see if that is double code in your project.在错误消息中,检查它到底在哪里抱怨,看看这是否是您项目中的双重代码。

I also faced same error only on prod build but not on dev build.我也只在 prod 构建上遇到了同样的错误,而在 dev 构建上没有。 I imported component.ts file in app.module.ts but didn't used in NgModule.我在 app.module.ts 中导入了 component.ts 文件,但没有在 NgModule 中使用。 So I removed the import, it worked fine.所以我删除了导入,它工作正常。

In my case, there were two components what are named same.就我而言,有两个组件名称相同。 I removed one of them and it's fixed.我删除了其中之一,它已修复。

通常,当您创建组件但未将其导入任何模块时会发生此错误。

确保在@NgModule 中导入您的组件。

I was facing the same error on production build also, with a my class ToastComponent.我在生产构建中也遇到了同样的错误,我的类 ToastComponent。 I've ended up with not exporting the class (ToastComponent) and use it like so:我最终没有导出类(ToastComponent)并像这样使用它:

class ToastComponent extends Toast {
  options: ToastIndividualConfig;
}

interface ToastIndividualConfig extends IndividualConfig {
  template: TemplateRef<any>;
  type: string;
}
@Component({
  selector: 'template-toast-component',
  templateUrl: './template-toast.component.html',
  styleUrls: ['./template-toast.component.scss'],
})
export class TemplateToastComponent extends ToastComponent {

暂无
暂无

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

相关问题 Angular2,AoT编译:无法确定组件AppComponent的模块 - Angular2, AoT compilation: Cannot determine the module for component AppComponent Angular AOT编译错误“无法确定类组件的模块” - Angular AOT compilation error "cannot determine module for class Component'' Angular 7-生产构建错误-无法确定X类组件的模块! 将X组件添加到NgModule中进行修复 - Angular 7 - Production Build Error - Cannot determine the module for class X Component! Add X Component to the NgModule to fix it 错误:无法确定组件页面的模块 - Error: Cannot determine the module for component Page 找不到模块 Angular 2 @Component - Cannot find module Angular 2 @Component Angular 6,无法将组件导入到routing.module - Angular 6, Cannot import component into routing.module 有没有办法禁用有关“无法确定组件模块...”的错误? - Is there a way to disable error regarding “Cannot determine the module for component…”? Angular 2 ERROR in无法确定类ManagersService i的模块 - Angular 2 ERROR in Cannot determine the module for class ManagersService i 无法确定 class Component.ts 文件中的模块,将组件添加到 NgModule 中修复它 - Cannot determine the module for class Component in component.ts file, add component to the NgModule to fix it 无法确定模块类x位置/ x.component.ts中的组件将x组件添加到Ng模块以修复它。 Angular 4 - Can not determine the module class x Component in location/x.component.ts add x component to Ng Module to fix it. Angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM