简体   繁体   English

使用带路由的ngUpgrade引导Angular 2 rc.6混合应用程序

[英]Bootstrapping an Angular 2 rc.6 hybrid app using ngUpgrade with routing

I have an Angular 1/Angular 2 hybrid app that was working with rc.3 and the deprecated router. 我有一个Angular 1 / Angular 2混合应用程序正在使用rc.3和不推荐使用的路由器。 From all sources that I can find, rc.5 is the big step to move towards with the new router. 从我能找到的所有资源中,rc.5是迈向新路由器的重要一步。 I am able to get my hybrid app bootstrapped, and render my root component, but routing does not work. 我可以启动我的混合应用程序,并渲染我的根组件,但路由不起作用。

var upgradeAdapter = new UpgradeAdapter(forwardRef(() => AppModule));

angular.module('ng1App', [])
  .directive('myBaseComponent', <any>upgradeAdapter.downgradeNg2Component(MyBaseComponent));

@NgModule({
  imports: [BrowserModule, routing],
  providers: [appRoutingProviders, HTTP_PROVIDERS],
  declarations: [MyBaseComponent,
    MyNG2RoutableComponent]
})
class AppModule { }


upgradeAdapter.bootstrap(document.body, ['ng1App']).ready(function(){
  console.log('bootstraped!');
});

My root NG2 component bootstraps, as I can throw stuff in the template it renders. 我的根NG2组件引导,因为我可以在它呈现的模板中抛出东西。 But it seems when I add the <router-outlet></router-outlet> it will not render the child route. 但是当我添加<router-outlet></router-outlet>它似乎不会呈现子路由。 If I bootstrap just my NG2 app without the upgradeAdapter, everything works as expected. 如果我在没有upgradeAdapter的情况下仅启动我的NG2应用程序,一切都按预期工作。

I feel like I am missing just one connecting piece. 我觉得我只缺少一个连接件。 Any ideas? 有任何想法吗?


Angular RC.6 and Router RC.2 Update Angular RC.6和路由器RC.2更新

I upgraded to rc.6 and the rc.2 version of the router this past week, same problem. 我上周升级到rc.6和rc.2版本的路由器,同样的问题。 The UpgradAdapter works great when routing isn't involved. 当不涉及路由时,UpgradAdapter非常有用。 Once I pull in the router-outlet, then nothing renders and there are no errors. 一旦我拉入路由器插座,然后没有任何渲染,没有错误。

The Angular Router requires at least one component be bootstrapped in order to be instantiated. 角度路由器需要至少一个组件被引导才能被实例化。 Since ngUpgrade bootstraps the Angular 1 side, there isn't an Angular 2 bootstrapped component. 由于ngUpgrade引导Angular 1侧,因此没有Angular 2引导组件。 To workaround this, you have to override the ApplicationRef and provide your own component. 要解决此问题,您必须覆盖ApplicationRef并提供自己的组件。 Here is a working plunker demonstrating how to achieve this. 这是一个工作的plunker演示如何实现这一目标。

http://plnkr.co/edit/Gj8xq0?p=preview http://plnkr.co/edit/Gj8xq0?p=preview

For reference: 以供参考:

https://github.com/angular/angular/issues/9870 https://github.com/angular/angular/issues/9870

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM