【问题标题】:loading two components in angular2 non SPA在 angular2 非 SPA 中加载两个组件
【发布时间】:2016-11-16 13:40:08
【问题描述】:

给定以下组件。 我想在非 SPA 网站上使用这些组件,就像 plunker here 一样。此示例不再有效,因为 Angular 2 的最新 beta 版使用模块,不再定义引导程序,我无法让这两个组件并行工作。

// 我们的第一个组件。

import {Component} from 'angular2/core'

@Component({
  selector: 'comp1',
  providers: [],
  template: `
    <div>
      <h3>Hello, {{name}}</h3>

    </div>
  `,
  directives: []
})
export class Comp1 {
  constructor() {
    this.name = 'I am ng2 component 1'
  }
}

// 我们的第二个组件。

import {Component} from 'angular2/core'

@Component({
  selector: 'comp2',
  providers: [],
  template: `
    <div>
      <h3>Hello, {{name}}</h3>

    </div>
  `,
  directives: []
})
export class Comp2 {
  constructor() {
    this.name = 'I am ng2 component 2'
  }
}

有一个plunker here,使用 angular 的 beta 版本。 如何使用最新版本的 Angular 2 注册这两个组件。该示例不再适用于较新的 Angular 版本。

当我尝试导入引导程序时,新版本的 angular 结构不同。

import {platform} from '@angular/core';
// platform is undefined.
import { bootstrap, BROWSER_PROVIDERS, BROWSER_APP_PROVIDERS } from '@angular/platform-browser';
// bootstrap is undefined, BROWSER_PROVIDERS is undefined etc.

【问题讨论】:

    标签: angular


    【解决方案1】:

    其他人尝试这个的答案是创建两个模块,register each module separately

    注册

    //main entry point
    import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
    import {AppModuleA, AppModuleB} from './app';
    
    platformBrowserDynamic().bootstrapModule(AppModuleA);
    platformBrowserDynamic().bootstrapModule(AppModuleB);
    

    模块

    @NgModule({
      imports: [ BrowserModule ],
      declarations: [ AppA ],
      bootstrap: [ AppA ]
    })
    export class AppModuleA {}
    
    @NgModule({
      imports: [ BrowserModule ],
      declarations: [ AppB ],
      bootstrap: [ AppB ]
    })
    export class AppModuleB {}
    

    【讨论】:

      【解决方案2】:

      您似乎缺少模块定义,例如:

      app.module.ts

      import { NgModule } from '@angular/core';
      import { BrowserModule } from '@angular/platform-browser';
      
      import {Comp1} from './comp1';
      import {Comp2} from './comp2';
      
      @NgModule({
          imports: [
              BrowserModule
          ],
          declarations: [
              Comp1,
              Comp2
          ],
          bootstrap: [Comp1]
      })
      
      export class AppModule { }
      

      ma​​in.ts

      import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
      
      import { AppModule } from './app.module';
      
      platformBrowserDynamic().bootstrapModule(AppModule);
      

      【讨论】:

      • 你说的很有道理,但是我似乎无法将这两个组件用作对等组件。 静态内容,如在 plunker 中。 angular2 似乎只适用于 SPA 应用程序。
      • 我不太确定您想要实现什么,但据我了解,您需要有一个主要组件来引导。它可以是路由器在您的 2 个组件之间切换的简单包装。
      • 我想要实现的和plunker一模一样,只是angular2最新版本,plunker来自旧版本。如果我只是将 Angular 版本更新为 beta.17,我会收到错误消息。 core_1.platform 不是函数。
      • 好的,我明白了。很抱歉造成误解。
      猜你喜欢
      • 2017-07-08
      • 2016-06-22
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多