2015-08-08 83 views
2

我期待从另一个模块以及窗口对象访问Angular 2组件。窗口对象仅用于在浏览器控制台中播放,而不是实际用于生产(fyi)。我想我可以'导出默认类MyComponent',但似乎并不奏效。我将如何从另一个模块/窗口访问实例化的MyAppComponent类?从另一个模块/窗口访问Angular 2组件

<body> 
    <my-app></my-app> 
    <script> 
     System.import('ang').then(function (ang) { 
      window.ang = ang; 
     }); 
    </script> 
</body> 

...

import {Component, View, bootstrap} from 'angular2/angular2'; 

@Component({ 
    selector: 'my-app' 
}) 
@View({ 
    template: `<h1>Hello {{ count }}</h1><br>` 
}) 

class MyAppComponent { 
    public count: number; 

    constructor() { 
     this.count = 0; 
    } 

    public increment(): void { 
     this.count++; 
    } 
} 

bootstrap(MyAppComponent); 

然后想要做这样的事情:

bootstrap(AppComponent, [ 
    ROUTER_PROVIDERS, 
]).then((app)=> { 
    window.app = app 
}); 

然后从拨打电话:

ang.increment(); 

回答

0

您可以从引导回调做控制台

app.instance.myMethod() 
相关问题