账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    如何搭建 angular2 多应用项目?
    36
    0

    如何使用webpack + ES6 + angular2 搭建一个多应用后台管理系统,要如何实现后端渲染?
    有没有一些开源的项目推荐或是学习资料等。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 队友在哪 普通会员 1楼

      要搭建 Angular 2 多应用项目,您需要按照以下步骤进行:

      1. 安装 Angular CLI:在命令行中,使用以下命令安装 Angular CLI:

      npm install -g @angular/cli

      1. 创建新项目:在命令行中,使用以下命令创建一个新的 Angular 2 项目:

      ng new multi-app

      1. 配置项目:在命令行中,使用以下命令配置项目:

      cd multi-app ng serve

      这将启动一个开发服务器,您可以在这里查看和编辑代码。

      1. 创建多个应用:在项目中,您将看到多个应用的文件夹,每个应用都代表一个独立的应用。要创建多个应用,请使用以下命令:

      ng generate app --name-app1 app1 --name-app2 app2

      这将在项目中创建两个应用,每个应用都包含一个特定的文件夹。

      1. 依赖注入:为了使多个应用相互通信,您需要在每个应用中注入依赖。您可以在每个应用中使用 @angular/core@angular/common 模块的依赖注入服务。例如,在 app1.component.ts 文件中:

      ```typescript import { Component, OnInit } from '@angular/core';

      @Component({ selector: 'app-app1', templateUrl: './app1.component.html', styleUrls: ['./app1.component.css'] }) export class App1Component implements OnInit { constructor() { }

       ngOnInit(): void { }
      

      } ```

      app2.component.ts 文件中:

      ```typescript import { Component, OnInit } from '@angular/core';

      @Component({ selector: 'app-app2', templateUrl: './app2.component.html', styleUrls: ['./app2.component.css'] }) export class App2Component implements OnInit { constructor() { }

       ngOnInit(): void { }
      

      } ```

      1. 通信:为了使两个应用之间通信,您可以使用 @angular/core 模块的通信服务。例如,在 app1.component.ts 文件中:

      ```typescript import { Component, OnInit } from '@angular/core'; import { App2Service } from './app2.service';

      @Component({ selector: 'app-app1', templateUrl: './app1.component.html', styleUrls: ['./app1.component.css'] }) export class App1Component implements OnInit { constructor(private app2Service: App2Service) { }

       ngOnInit(): void { }
      

      }

      @Component({ selector: 'app-app2', templateUrl: './app2.component.html', styleUrls: ['./app2.component.css'] }) export class App2Component implements OnInit { constructor(private app1Service: App1Service) { }

       ngOnInit(): void { }
      

      }

      ngOnInit(): void { this.app2Service.getMessage(); } ```

      app2.component.ts 文件中:

      ```typescript import { Component, OnInit } from '@angular/core'; import { App1Service } from './app1.service';

      @Component({ selector: 'app-app2', templateUrl: './app2.component.html', styleUrls: ['./app2.component.css'] }) export class App2Component implements OnInit { constructor(private app1Service: App1Service) { }

       ngOnInit(): void { }
      

      }

      constructor(private app1Service: App1Service) { }

      ngOnInit(): void { this.app1Service.getMessage(); } ```

      1. 安装和运行:在命令行中,使用以下命令安装并运行应用:

      npm install ng serve

      这将启动一个开发服务器,并在本地浏览器中打开 http://localhost:4200。您可以在这里查看和编辑代码。

      以上步骤将帮助您搭建 Angular 2 多应用项目。请注意,每个应用可能需要一些额外的配置和扩展,以满足特定的需求。

    • 放烟火的小笨蛋 普通会员 2楼

      要搭建 Angular 2 多应用项目,您需要按照以下步骤进行:

      1. 安装 Angular CLI:在命令行中,使用以下命令安装 Angular CLI:

      npm install -g @angular/cli

      1. 创建新项目:在命令行中,使用以下命令创建一个新的 Angular 2 项目:

      ng new multi-app

      1. 配置项目:在命令行中,使用以下命令配置项目:

      cd multi-app ng serve

      这将启动一个开发服务器,您可以在这里查看和编辑代码。

      1. 创建多个应用:在项目中,您将看到多个应用的文件夹,每个应用都代表一个独立的应用。要创建多个应用,请使用以下命令:

      ng generate app --name-app1 app1 --name-app2 app2

      这将在项目中创建两个应用,每个应用都包含一个特定的文件夹。

      1. 依赖注入:为了使多个应用相互通信,您需要在每个应用中注入依赖。您可以在每个应用中使用 @angular/core@angular/common 模块的依赖注入服务。例如,在 app1.component.ts 文件中:

      ```typescript import { Component, OnInit } from '@angular/core';

      @Component({ selector: 'app-app1', templateUrl: './app1.component.html', styleUrls: ['./app1.component.css'] }) export class App1Component implements OnInit { constructor() { }

       ngOnInit(): void { }
      

      } ```

      app2.component.ts 文件中:

      ```typescript import { Component, OnInit } from '@angular/core';

      @Component({ selector: 'app-app2', templateUrl: './app2.component.html', styleUrls: ['./app2.component.css'] }) export class App2Component implements OnInit { constructor() { }

       ngOnInit(): void { }
      

      } ```

      1. 通信:为了使两个应用之间通信,您可以使用 @angular/core 模块的通信服务。例如,在 app1.component.ts 文件中:

      ```typescript import { Component, OnInit } from '@angular/core'; import { App2Service } from './app2.service';

      @Component({ selector: 'app-app1', templateUrl: './app1.component.html', styleUrls: ['./app1.component.css'] }) export class App1Component implements OnInit { constructor(private app2Service: App2Service) { }

       ngOnInit(): void { }
      

      }

      @Component({ selector: 'app-app2', templateUrl: './app2.component.html', styleUrls: ['./app2.component.css'] }) export class App2Component implements OnInit { constructor(private app1Service: App1Service) { }

       ngOnInit(): void { }
      

      }

      ngOnInit(): void { this.app2Service.getMessage(); } ```

      app2.component.ts 文件中:

      ```typescript import { Component, OnInit } from '@angular/core'; import { App1Service } from './app1.service';

      @Component({ selector: 'app-app2', templateUrl: './app2.component.html', styleUrls: ['./app2.component.css'] }) export class App2Component implements OnInit { constructor(private app1Service: App1Service) { }

       ngOnInit(): void { }
      

      }

      constructor(private app1Service: App1Service) { }

      ngOnInit(): void { this.app1Service.getMessage(); } ```

      1. 安装和运行:在命令行中,使用以下命令安装并运行应用:

      npm install ng serve

      这将启动一个开发服务器,并在本地浏览器中打开 http://localhost:4200。您可以在这里查看和编辑代码。

      以上步骤将帮助您搭建 Angular 2 多应用项目。请注意,每个应用可能需要一些额外的配置和扩展,以满足特定的需求。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部