angular - 在两个不同的模块之间共享一个组件

标签 angular angular-components angular-module

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

enter image description here enter image description here

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

student-rating.component.html 和 student-rating.component.ts

<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>

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 ---> 它不包含任何关于 student-rating.component.ts 的组件。 enter image description here

这些是 app.module.ts 的声明

declarations: [AppComponent, StudentRatingsComponent,Page404Component, CapitalizePipe],

最佳答案

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

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

@NgModule({
declarations: [AComponent],
exports: [AComponent]
});
export class module1;

现在如果你需要在module2中使用这个组件,你不在module2中声明那个组件,你在module2中导入module1,

@NgModule({
imports: [module1]
})
export class module2;

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

关于angular - 在两个不同的模块之间共享一个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53863816/

相关文章:

Angular 2 ngClass - 应用多个类,带有类变量

javascript - 如何访问 Angular 组件 $postLink 内的元素

angular - 如何选择组件模板中的元素?

angular - 使用环境为ng服务设置proxy.config.js属性

c# - 发布网站时出现 Angular 未知提供商错误

angular - 在 <tr> 中嵌入 Angular 指令

angular - Electron中的Angular服务从Express错误获取数据

angular - 将本地开发的模块打包/导入到项目中

javascript - 初始化自定义类

angular - 将 Angular 响应式 FormControl 传递给子组件