【问题标题】:Wrap multiple modules in single Module Angular 2?在单个 Module Angular 2 中包装多个模块?
【发布时间】:2017-02-06 13:47:10
【问题描述】:

我有一个包含大约 20 个模块的组件库。我想将它们包装成一个服务并导出,以便可以全局注入。当我这样做并将其注入 app.module.ts 时,功能无法找到组件。

我的 CoreComponentModule 和所有其他模块看起来像这样。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { BreadcrumbModule } from './core/breadcrumb';
import { ButtonModule } from './core/button';
import { CheckboxModule } from './core/checkbox';
import { ChipsModule } from './core/chips';
import { DatepickerModule } from './core/datepicker';
import { ErrorMessagesModule } from './core/error-messages';
import { HeaderModule } from './core/header';
import { IconModule } from './core/icon';
import { InputModule } from './core/input';
import { MultiselectModule } from './core/multiselect';
import { ProgressSpinnerModule } from './core/progress-spinner';
import { RadioButtonModule } from './core/radio-button';
import { SelectModule } from './core/select';
import { SideNavModule } from './core/side-nav';
import { SlideToggleModule } from './core/slide-toggle';
import { SliderModule } from './core/slider';
import { TooltipModule } from './core/tooltip';
import { WizardModule } from './core/wizard';

const CORE_COMPONENT_MODULES = [
  CommonModule,
  BreadcrumbModule,
  ButtonModule,
  CheckboxModule,
  ChipsModule,
  DatepickerModule,
  ErrorMessagesModule,
  HeaderModule,
  IconModule,
  InputModule,
  MultiselectModule,
  ProgressSpinnerModule,
  RadioButtonModule,
  SelectModule,
  SideNavModule,
  SlideToggleModule,
  TooltipModule,
  WizardModule
]

@NgModule({
  imports: CORE_COMPONENT_MODULES,
  exports: CORE_COMPONENT_MODULES,
})
export class CoreComponentModule { }

我还有一个功能模块,它包含所有不同的功能,如下所示:-

import {NgModule} from '@angular/core';
import {LoginModule} from './auth';
import {DashboardModule} from './dashboard';
import {CoreComponentModule} from '../components';
@NgModule({
    imports:[
        CoreComponentModule,
        LoginModule,
        DashboardModule
    ],
    exports:[
        LoginModule,
        DashboardModule
    ]
})

export class ViewModule{

}

最后 app.module 如下所示:-

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppRoutingModule } from './app-routing.module';

import { CoreComponentModule } from './components';
import { ViewModule } from './views;

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    AppRoutingModule,
    CoreComponentModule,
    ViewModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我无法理解为什么这会引发有关组件的错误,并且我无法使用任何组件。我是否过度包装了组件。 我来自 Angular 2 Material git repo 的工具参考/灵感。如果我做错了什么,请指导我。

我得到的错误如下:-

Unhandled Promise rejection: Template parse errors:
'app-breadcrumb' is not a known element:
1. If 'app-breadcrumb' is an Angular component, then verify that it is part of this module.
2. If 'app-breadcrumb' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("<p>
  login works!
  [ERROR ->]<app-breadcrumb></app-breadcrumb>
</p>
"): LoginComponent@2:2 ; Zone: <root> ; Task: Promise.then ; Value: SyntaxError__zone_symbol__error: Error: Template parse errors:
'app-breadcrumb' is not a known element:
1. If 'app-breadcrumb' is an Angular component, then verify that it is part of this module.
2. If 'app-breadcrumb' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("<p>
  login works!
  [ERROR ->]<app-breadcrumb></app-breadcrumb>
</p>
"): LoginComponent@2:2
    at SyntaxError.ZoneAwareError (http://localhost:4200/vendor.bundle.js:96291:33)
    at SyntaxError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:73328:16)
    at new SyntaxError (http://localhost:4200/vendor.bundle.js:6048:16)
    at TemplateParser.parse (http://localhost:4200/vendor.bundle.js:20378:19)
    at JitCompiler._compileTemplate (http://localhost:4200/vendor.bundle.js:53611:68)
    at http://localhost:4200/vendor.bundle.js:53494:62
    at Set.forEach (native)
    at JitCompiler._compileComponents (http://localhost:4200/vendor.bundle.js:53494:19)
    at createResult (http://localhost:4200/vendor.bundle.js:53376:19)
    at ZoneDelegate.invoke (http://localhost:4200/vendor.bundle.js:95722:26)
    at Zone.run (http://localhost:4200/vendor.bundle.js:95593:43)
    at http://localhost:4200/vendor.bundle.js:96015:57
    at ZoneDelegate.invokeTask (http://localhost:4200/vendor.bundle.js:95755:35)
    at Zone.runTask (http://localhost:4200/vendor.bundle.js:95631:47)
    at drainMicroTaskQueue (http://localhost:4200/vendor.bundle.js:95913:35)__zone_symbol__message: "Template parse errors:↵'app-breadcrumb' is not a known element:↵1. If 'app-breadcrumb' is an Angular component, then verify that it is part of this module.↵2. If 'app-breadcrumb' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("<p>↵  login works!↵  [ERROR ->]<app-breadcrumb></app-breadcrumb>↵</p>↵"): LoginComponent@2:2"__zone_symbol__stack: "Error: Template parse errors:↵'app-breadcrumb' is not a known element:↵1. If 'app-breadcrumb' is an Angular component, then verify that it is part of this module.↵2. If 'app-breadcrumb' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("<p>↵  login works!↵  [ERROR ->]<app-breadcrumb></app-breadcrumb>↵</p>↵"): LoginComponent@2:2↵    at SyntaxError.ZoneAwareError (http://localhost:4200/vendor.bundle.js:96291:33)↵    at SyntaxError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:73328:16)↵    at new SyntaxError (http://localhost:4200/vendor.bundle.js:6048:16)↵    at TemplateParser.parse (http://localhost:4200/vendor.bundle.js:20378:19)↵    at JitCompiler._compileTemplate (http://localhost:4200/vendor.bundle.js:53611:68)↵    at http://localhost:4200/vendor.bundle.js:53494:62↵    at Set.forEach (native)↵    at JitCompiler._compileComponents (http://localhost:4200/vendor.bundle.js:53494:19)↵    at createResult (http://localhost:4200/vendor.bundle.js:53376:19)↵    at ZoneDelegate.invoke (http://localhost:4200/vendor.bundle.js:95722:26)↵    at Zone.run (http://localhost:4200/vendor.bundle.js:95593:43)↵    at http://localhost:4200/vendor.bundle.js:96015:57↵    at ZoneDelegate.invokeTask (http://localhost:4200/vendor.bundle.js:95755:35)↵    at Zone.runTask (http://localhost:4200/vendor.bundle.js:95631:47)↵    at drainMicroTaskQueue (http://localhost:4200/vendor.bundle.js:95913:35)"_nativeError: ZoneAwareErrormessage: (...)get message: ()set message: (value)name: (...)get name: ()set name: (value)originalStack: (...)get originalStack: ()set originalStack: (value)stack: (...)get stack: ()set stack: (value)toSource: ()toString: ()zoneAwareStack: (...)get zoneAwareStack: ()set zoneAwareStack: (value)__proto__: BaseError Error: Template parse errors:
'app-breadcrumb' is not a known element:
1. If 'app-breadcrumb' is an Angular component, then verify that it is part of this module.
2. If 'app-breadcrumb' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("<p>
  login works!
  [ERROR ->]<app-breadcrumb></app-breadcrumb>
</p>
"): LoginComponent@2:2
    at SyntaxError.ZoneAwareError (http://localhost:4200/vendor.bundle.js:96291:33)
    at SyntaxError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:73328:16)
    at new SyntaxError (http://localhost:4200/vendor.bundle.js:6048:16)
    at TemplateParser.parse (http://localhost:4200/vendor.bundle.js:20378:19)
    at JitCompiler._compileTemplate (http://localhost:4200/vendor.bundle.js:53611:68)
    at http://localhost:4200/vendor.bundle.js:53494:62
    at Set.forEach (native)
    at JitCompiler._compileComponents (http://localhost:4200/vendor.bundle.js:53494:19)
    at createResult (http://localhost:4200/vendor.bundle.js:53376:19)
    at ZoneDelegate.invoke (http://localhost:4200/vendor.bundle.js:95722:26)
    at Zone.run (http://localhost:4200/vendor.bundle.js:95593:43)
    at http://localhost:4200/vendor.bundle.js:96015:57
    at ZoneDelegate.invokeTask (http://localhost:4200/vendor.bundle.js:95755:35)
    at Zone.runTask (http://localhost:4200/vendor.bundle.js:95631:47)
    at drainMicroTaskQueue (http://localhost:4200/vendor.bundle.js:95913:35)
consoleError @ zone.js:420
_loop_1 @ zone.js:449
drainMicroTaskQueue @ zone.js:453

示例面包屑组件:-

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BreadcrumbComponent } from './breadcrumb.component';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    BreadcrumbComponent
  ],
  exports: [
    BreadcrumbComponent
  ]
})
export class BreadcrumbModule { }

文件夹结构的图像和作为模块的每个组件

【问题讨论】:

  • “为什么会抛出错误”如果您遇到错误,查看确切的错误消息会很有帮助
  • 如果上面的代码表明存在任何问题,则说明您在 app.module 中存在语法问题。您在导入 ViewModule 时缺少单引号。值得怀疑的是,这是否会为您解决所有这些问题。
  • @GünterZöchbauer 我更新了堆栈跟踪。
  • 面包屑组件在包含它的模块中导出?
  • @R.Richards 我猜这是在粘贴代码时发生的。基本上我希望将所有组件(它们本身就是一个模块)包装到一个公共模块中并导出。我也想对功能做同样的事情。

标签: javascript angular angular2-components


【解决方案1】:

组件不能全局可用。

使用组件、指令或管道的每个模块都需要在imports: [] 中列出提供这些的模块

仅在AppModule 中导入它们是不够的。

【讨论】:

  • 酷。这确实有效。现在很清楚了。所以组件是基本的构建块,可以在任何需要的地方导入。很好......另一件事是,如果全局需要某些东西,那么在这种情况下应该怎么做?
  • 不,不能在需要的地方导入组件。一个组件在一个 @NgModule() 中声明,并且每个想要使用该组件的模块都需要导入该模块。就是这样。
  • 每个组件都有模块。 github.com/angular/material2/…
  • 对不起,我不明白这个问题。您仍然收到此错误消息吗?
  • 你不能。材料组件还需要导入到使用其中之一的每个模块中。您可以拥有一个导出模块的模块,就像它导出组件一样。通过这种方式,您可以将功能模块组合成一个更大的功能模块,因此您只需导入一个模块即可获得一组可用模块的组件,但您仍然需要向要使用的模块添加至少一个导入任何东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 2016-12-24
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多