简体   繁体   English

如何将一个组件导入两个页面?

[英]How to import a component into two pages?

I have a structure like this:我有这样的结构:

  • home - page主页
  • home2 - page home2 - 页面
  • test-component - component测试组件 - 组件

I need to be able to use the component( <app-test-component></app-test-component> ) on both pages.我需要能够在两个页面上使用组件( <app-test-component></app-test-component> )。

If I just use the tag:如果我只使用标签:

ERROR Error: Uncaught (in promise): Error: Template parse errors: 'app-test-component' is not a known element:错误错误:未捕获(承诺):错误:模板解析错误:'app-test-component'不是已知元素:

If I just import ( import {TestComponentComponent} from '../test-component/test-component.component' ) into home.module.ts and put it in the module import:如果我只是将( import {TestComponentComponent} from '../test-component/test-component.component' )导入 home.module.ts 并将其放入模块导入中:

core.js:9110 ERROR Error: Uncaught (in promise): Error: Unexpected directive 'TestComponentComponent' imported by the module 'HomePageModule'. core.js:9110 错误错误:未捕获(承诺中):错误:模块“HomePageModule”导入的意外指令“TestComponentComponent”。 Please add a @NgModule annotation.请添加 @NgModule 注释。

A similar error if added to declarations.如果添加到声明中,则会出现类似的错误。

Git. Git。

How to import a component on both pages and use?如何在两个页面上导入组件并使用?

Declare TestComponentComponent only in home.module.仅在 home.module 中声明 TestComponentComponent。

home.module.ts home.module.ts

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild([
      {
        path: '',
        component: HomePage
      }
    ])
  ],
  declarations: [HomePage, TestComponentComponent]
})
export class HomePageModule {}

app.module.ts app.module.ts

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

There are few changes that you should make:您应该进行一些更改:

  1. You should not import component as I can see in the code.正如我在代码中看到的那样,您不应该导入component

  2. Rearrange the project structure, and move reusable component such as TestComponent into a shared component.重新安排项目结构,将可重用组件(如TestComponent )移动到共享组件中。 To give you an example, I'll give below example where I have created shared module names as TestModule举个例子,我将在下面给出我创建共享模块名称为TestModule的示例

/app
  /home
   -home.component.ts
   -home.module.ts

  /test
   -test.component.ts
   -test.module.ts

-app.component.ts
-app.module.ts

app.module.ts app.module.ts

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, HomeModule, RouterModule.forRoot([{ path: '', component: HomeComponent }])],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

test.module.ts测试模块.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TestComponent } from './test.component';

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

home.module.ts home.module.ts


@NgModule({
  declarations: [HomeComponent],
  exports: [HomeComponent],
  imports: [
    CommonModule,
    TestModule
  ]
})
export class HomeModule { }

This set of project structure would certainly help you to reuse test.module features.这套项目结构肯定会帮助您重用test.module功能。 Let me know if you need more code.如果您需要更多代码,请告诉我。

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

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