如何在两个不同的模块中声明AppComponent

时间:2019-06-25 11:47:42

标签: angular typescript

我正在运行Angular 7项目,其中包含两个不同的应用程序:

  1. BasicTools
  2. AdvancedTools

BasicTools项目指向basic-main.ts,而AdvancedTools项目指向advanced-main.ts。从basic-main.ts文件中引导BasicAppModule,从Advanced-main.ts中引导AdvancedAppModule。

我有在BasicAppModule和AdvancedAppModule中都使用的通用AppComponent,但是在构建应用程序时,由于AppComponent被声明为两个模块的一部分,所以我遇到了错误。

我尝试在basic-app.module.ts中导出AppComponent,还从advanced-app.module.ts中删除了AppComponent的声明,但出现错误,因为:AppComponent应该声明为ngModule的一部分。

有什么方法可以解决此问题,因为两个模块中都需要AppComponent。??

下面是我的angular.json文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "BasicTools": {
      "root": "",
      "sourceRoot": "Client",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "wwwroot",
            "index": "Client/index.html",
            "main": "Client/app/basic/basic-main.ts",
            "polyfills": "Client/polyfills.ts",
            "tsConfig": "Client/tsconfig.app.json",
            "assets": [
              "Client/assets"
            ],
            "styles": [
              "Client/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "Client/environments/environment.ts",
                  "with": "Client/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "none",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "BasicTools:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "BasicTools:build:production"
            }
          }
        }
    },
    "AdvancedTools": {
      "root": "",
      "sourceRoot": "Client",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "wwwroot",
            "index": "Client/index.html",
            "main": "Client/app/advanced/advanced-main.ts",
            "polyfills": "Client/polyfills.ts",
            "tsConfig": "Client/tsconfig.app.json",
            "assets": [
              "Client/assets"
            ],
            "styles": [
              "Client/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "Client/environments/environment.ts",
                  "with": "Client/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "none",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "AdvancedTools:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "AdvancedTools:build:production"
            }
          }
        }
    },

advanced-main.ts

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { environment } from '../../environments/environment';
import { AdvancedAppModule } from '../advanced-app.module';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AdvancedAppModule);

basic-main.ts

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { environment } from '../../environments/environment';
import { BasicAppModule } from '../basic-app.module';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(BasicAppModule);

advanced-app.module.ts

@NgModule({
  declarations: [ AppComponent ],
  imports: [
    BrowserModule,
    CoreModule,
    SharedModule,
    BasicModule,
    AdvancedModule,
    applicationRoutingInformation
  ],
  providers: [
    { provide: LocationStrategy, useClass: MiPathLocationStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AdvancedAppModule { }

basic-app.module.ts

@NgModule({
  declarations: [ AppComponent ],
  imports: [
    BrowserModule,
    BasicModule,
    CoreModule,
    SharedModule,
    applicationRoutingInformation
  ],
  providers: [
    { provide: LocationStrategy, useClass: MiPathLocationStrategy }
  ],
  bootstrap: [AppComponent]
})
export class BasicAppModule { }

2 个答案:

答案 0 :(得分:1)

创建导出AppModule的{​​{1}}并将其导入您的两个根模块配置中:

app.module.ts

AppComponent

basic-app.module.ts

@NgModule({
  declarations: [AppComponent],
  exports: [AppComponent]
})
export AppModule {}

advanced-app.module.ts

@NgModule({
  imports: [AppModule, ...],
  bootstrap: [AppComponent],
  ...
})
export class BasicAppModule{}

即使您在这两个输入模块之间共享一些通用的AppComponent依赖性,也可以在@NgModule({ imports: [AppModule, ...], bootstrap: [AppComponent], ... }) export class AdvancedAppModule {} 配置中为其添加提供程序。

答案 1 :(得分:0)

我们不能在两个不同的组件中声明同一组件,在这种情况下,我们将创建一个公共模块并将其导入两个模块中。

common-module.ts

import { NgModule } from '@angular/core';
//import your component


@NgModule({
  declarations:[yourComponent],
  exports: [yourComponent]
})

export class CommonModule { }