简体   繁体   English

Angular 2重用带有共享模块的组件

[英]Angular 2 Reusing Components with Shared Modules

I am trying to reduce redundant code in my application by consolidating components, used in multiple modules, into a shared module. 我试图通过将多个模块中使用的组件合并为一个共享模块来减少应用程序中的冗余代码。 I was following the Angular Post, https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module . 我正在关注Angular Post, 网址为https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module It appears that I am missing a step because the application hangs when I make the described changes. 看来我错过了一步,因为进行上述更改时应用程序挂起。

I have created two plunkers, one without Shared Module and a second plunker after the changes have been made. 更改完成后,我创建了两个插件,一个没有共享模块,另一个是插件。 The initial code can be found at this link; 初始代码可以在此链接上找到; https://plnkr.co/edit/VrEe5S54rEkKipuGLsQs?p=info https://plnkr.co/edit/VrEe5S54rEkKipuGLsQs?p=info

I then made the following changes. 然后,我进行了以下更改。

  1. Created Shared Module (See code below) 创建共享模块(请参见下面的代码)
  2. Added PageNotFoundComponent to SharedModule 将PageNotFoundComponent添加到SharedModule
  3. Updated app.module.ts 更新了app.module.ts

    a. 一种。 Commented out FormsModule 注释掉FormsModule

    b. b。 Commented out PageNotFoundComponent 注释掉PageNotFoundComponent

    c. C。 Imported SharedModule 导入的SharedModule

  4. Updated app-routing.module.ts 更新了app-routing.module.ts

    a. 一种。 Commented out PageNotFoundComponent 注释掉PageNotFoundComponent

Note that I tried to keep it simple by not adding too many components to the shared module and by not using shared module everywhere that it could have been used. 请注意,我尝试通过不向共享模块中添加太多组件以及不在可能已使用的所有地方使用共享模块来简化操作。

The Plunker with these changes can be found at this link; 具有这些更改的Plunker可以在此链接上找到; https://plnkr.co/edit/I4ArEQTniO7MJtfzpBWl?p=info https://plnkr.co/edit/I4ArEQTniO7MJtfzpBWl?p=info

The code for the Shared Module is as follows: 共享模块的代码如下:

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

import { PageNotFoundComponent }     from './not-found.component';

@NgModule({
    imports:      [ CommonModule,
                    FormsModule],
    declarations: [ PageNotFoundComponent
                  ],
    exports:      [ PageNotFoundComponent,
                    CommonModule,
                    FormsModule ]
})
export class SharedModule { }

Appreciate any thoughts on what steps I am missing or what I am doing wrong. 感谢您对我缺少哪些步骤或我做错了什么的任何想法。

The error is: 错误是:

Error loading https://run.plnkr.co/5Q7FuGzCTzE1DQOa/app/shared/not-
found.component.ts as "./not-found.component" from 
https://run.plnkr.co/5Q7FuGzCTzE1DQOa/app/shared/shared.module.ts

So shared.module.ts is in the 'app/shared' folder, but it's trying to load the not-found.component from the same directory. 因此shared.module.ts位于“ app / shared”文件夹中,但是它试图从同一目录加载notfound.component。

Looking at your plunker, the not-found.component is in the 'app' folder. 看着您的监听器,notfound.component位于“ app”文件夹中。

I believe you need to move the not-found component to the 'app/shared' folder. 我相信您需要将未找到的组件移至“ app / shared”文件夹。

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

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