首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在两个不同的模块之间共享组件

在两个不同的模块之间共享组件
EN

Stack Overflow用户
提问于 2018-12-20 14:58:45
回答 3查看 3K关注 0票数 2

在我的application.one中有两个模块是雇主和第二个是登陆。我已经在登陆模块中创建了一个组件,我想与雇主模块共享这个组件。为此,我在父模块中的app.module.ts中声明了这个组件,并在子模块中使用它们。

如果我在单个模块中使用它,它已经可以工作了,但是当我在不同的模块中共享它时,它会显示错误

student-student ing.Component.html和student-student ing.Component.ts

代码语言:javascript
复制
<div class="stu_profile_ratings">
          <h3>Average Ratings</h3>
            <!-- <rating [(ngModel)]="performance" [disabled]="false"  [readonly]="true" [required]="true">
              </rating> -->
              <a (click)="showHideDrop();"> <img src ="../../../assets/images/drop-down-arrow-white.png" /></a> 
              <div *ngIf="showRatings" class="ratings_dropdown ">
                <h4>Ratings given by verified employers</h4>
                  <ul class="marginT10">
                    <li>
                      <h5>Performance</h5>
                     <!--  <rating [(ngModel)]="performance" [disabled]="false"  [readonly]="true" [required]="true"></rating> -->
                    </li>
                    <li>
                      <h5>Work Quality</h5>
               <!--        <rating [(ngModel)]="work_quality" [disabled]="false"  [readonly]="true" [required]="true"></rating> -->
                    </li>
                    <li>
                      <h5>Professionalism</h5>
                   <!--    <rating [(ngModel)]="professionalism" [disabled]="false"  [readonly]="true" [required]="true"></rating> -->
                    </li>
                  </ul>
              </div>

代码语言:javascript
复制
import { Component, OnInit ,Input, ChangeDetectorRef} from '@angular/core';
declare var $: any;
@Component({
  selector: 'app-student-ratings',
  templateUrl: './student-ratings.component.html',  
styleUrls: ['./student-ratings.component.css']
})
export class StudentRatingsComponent implements OnInit {
  showRatings : boolean = false;
  @Input() performance:string;
  @Input() work_quality:string;  
@Input() professionalism:string;
constructor() {  }
  ngOnInit() {  }
  showHideDrop(){
this.showRatings = !this.showRatings;
  }
}

landing.module.ts ->它不包含任何关于学生评分的组件。

以下是app.module.ts的声明

代码语言:javascript
复制
declarations: [AppComponent, StudentRatingsComponent,Page404Component, CapitalizePipe],

EN

回答 3

Stack Overflow用户

发布于 2018-12-20 15:11:09

如果需要在另一个模块中使用组件,则只能在一个模块中声明该组件。从声明该组件的模块中导出该组件,并将该模块导入要使用该组件的模块中。

例如,您有一个名为AComponent的组件,您有三个模块(module1、module2和appModule)。

代码语言:javascript
复制
@NgModule({
declarations: [AComponent],
exports: [AComponent]
});
export class module1;

现在,如果您需要在module2中使用此组件,则不必在module2中声明该组件,而是在module2中导入module1,

代码语言:javascript
复制
@NgModule({
imports: [module1]
})
export class module2;

有关更多信息,请参阅官方文档https://angular.io/guide/sharing-ngmodules

票数 6
EN

Stack Overflow用户

发布于 2018-12-20 15:10:33

对于第一个屏幕截图问题read this.

app.module.ts文件中删除StudentRatingsComponent。它将修复第二个屏幕截图问题。

票数 1
EN

Stack Overflow用户

发布于 2018-12-20 15:21:02

在着陆模块中添加exports属性,并导出要与其他模块共享的组件。在你的应用模块中,只需导入你的登陆模块即可。

考虑下面的类是您的LandingModule

代码语言:javascript
复制
@NgModule({

declarations:[StudentsRatingComponent],
exports:[StudentsRatingComponent]
})
export class LandingModule{}

只需在应用程序模块中导入LandingModule即可

代码语言:javascript
复制
@NgModule({
...
imports:[LandungModule],
...
})
export class AppModule{}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53863816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档