我希望能够从另一个模块以及window对象中访问Angular 2组件。window对象仅用于在浏览器控制台中进行调试,而不是实际生产(供您参考)。我认为我可以使用“export default class MyComponent”,但似乎不起作用。如何从另一个模块/窗口中访问已实例化的MyAppComponent类?
<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);
然后想要做这样的事情:
ang.increment();