【问题标题】:How to access angular-component in ionic-page?如何访问离子页面中的角度组件?
【发布时间】:2018-09-22 21:10:11
【问题描述】:

我已经建立了一个离子项目并添加了一个页面“sumbuilder”。之后,我添加了一个组件“partscount”。该组件已自动添加到 app.module.ts,但我没有设法让标签“<app-partscount></app-partscount>”在我的页面中运行。所以我从 app.module.ts 中删除了组件声明,将它移到我的 sumbuilder.module.ts 中,然后它就可以工作了。

这是工作代码:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';

import { IonicModule } from '@ionic/angular';

import { SumbuilderPage } from './sumbuilder.page';
import { PartcountsComponent} from '../partcounts/partcounts.component';

const routes: Routes = [
  {
    path: '',
    component: SumbuilderPage
  }
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild(routes)
  ],
  declarations: [
    SumbuilderPage, 
    PartcountsComponent
  ]
})
export class SumbuilderPageModule {}

现在的问题是:当在 app.module.ts 中声明组件时,我必须在此文件 (sumbuilder.module.ts) 中添加什么才能访问组件?我是 Angular 的新手,当然,这是一个初学者的问题。因为想多次使用这个组件,所以想在app.module中声明一下。

app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { PartcountsComponent } from './partcounts/partcounts.component';
import { FormsModule } from '@angular/forms';

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

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    如果我正确理解您的问题,您的意思是我们如何在一个模块中声明一个组件并在另一个模块中使用它。如果你是这样想的,这里就是答案:

    每个 Angular 模块都必须使用 declaration: []@NgModule 装饰器指定其声明的组件。如果该模块允许其他模块使用其组件,则必须在@NgModuleexports: [] 中指定。在这种情况下,另一个模块可以导入该模块并使用其导出的组件。这是一个例子:

    @Component({
      selector: 'first-component',
      template: `<p> I'm first component </p>`
    })
    export class FirstComponent {}
    
    @NgModule({
      declarations: [ FirstComponent ],
      exports: [ FirstComponent ]
    })
    export class MyFirstModule {}
    

    在另一个模块中,我们可以使用导出的FirstComponent of MyFirstModule

    @NgModule({
      declarations: [ ComponentWhichUsesFirstComponent ],
      imports: [ MyFirstModule ]
    })
    export class MySecondModule {}
    
    @Component({
      selector: 'an-optional-selector',
      template: `<first-component>  </first-component>`
    })
    export class ComponentWhichUsesFirstComponent {}
    

    请参阅此 stackblits 示例。

    【讨论】:

    • 感谢您的回答。我认为你接近我的解决方案。如果我在 app.module.ts 中声明了该组件,现在我想从我的页面访问该组件怎么办。我必须在我的 sumbuilder.module.ts 中导入“AppModule”吗?
    • 是的,任何模块都存在导入/导出规则。
    • 我建议在 sumbuilder.module.ts 中声明和导出你的组件,然后在 app.module.ts 中导入使用
    • 你能解释一下为什么吗?我以为你只需要在 appmodule 中声明一次组件,然后你就可以从任何地方访问它。这是我想做的。当我在 sumbuilder.module.ts 中声明组件时,我没有问题。
    • 阅读本文以更深入地了解 Angular 模块:medium.com/@cyrilletuzi/…
    猜你喜欢
    • 2018-01-28
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多