Angular2 嵌套路由 parent 到 child ES5 不工作

Angular2 Nested Routing parent to child ES5 not working

目前正在使用 angular2 beta 版本 6,在这种嵌套路由中,例如 parent 到 child 使用 (EX: /plan/...) future 不适用于 es5 JavaScript 开发,但在类型脚本开发中它运行完美

抛出错误:异常:Link“["Plan"]”未解析为终端指令。

App.js代码

var Tabs = [],viewId;

app.AppComponent =
    ng.core.Component({
        selector: 'app',
        template: '<router-outlet></router-outlet>',
        directives: [ng.router.ROUTER_DIRECTIVES],
         providers: [ng.http.HTTP_PROVIDERS]
    })

    .Class({

        constructor: [ng.router.Router, function(router) {
            console.log("1");
            router.config([
                { path: '/', redirectTo: ['Home']  },
                { path: '/home', component: app.HomeComponent, name: 'Home'  },
                { path: '/plan/...', component: app.planComponent, name: 'Plan' }
            ]);
        }]
    });

document.addEventListener('DOMContentLoaded', function() {
    ng.platform.browser.bootstrap( app.AppComponent,[ng.router.ROUTER_PROVIDERS]);
});

Plan.js代码

app.planComponent =
    ng.core.Component({
        selector: 'plan-view',
        templateUrl: './assets/src/plan/view/plan.html',
        directives: [ ng.router.RouterLink, ng.router.ROUTER_DIRECTIVES],
    })
    .Class({
        constructor:[ng.router.Router, function(router){
            console.log("2");
           router.config([
                { path: '/', redirectTo: ['PlanInfo'] },
                { path: '/planInfo', component: app.planInfoComponent, name: 'PlanInfo', useAsDefault: true  }/*,
                { path: '/coverage', component: app.CoverageComponent, name: 'Coverage'  },
                { path: '/nonelective', component: app.nonElectiveComponent, name: 'NonElective'  },
                { path: '/loans', component: app.loansComponent, name: 'Loans'  },
                { path: '/enrollment', component: app.enrollmentComponent, name: 'Enrollment'  }*/
            ]);
        }]
    });

我认为您应该使用 ng.router.RouteConfig 装饰器而不是路由器本身。这样,您将拥有与 TypeScript 相同的配置。

方法如下:

ng.router
    .RouteConfig([
      { path: '/', redirectTo: ['Home']  },
      { path: '/home', component: app.HomeComponent, name: 'Home'  },
      { path: '/plan/...', component: app.planComponent, name: 'Plan' }
    ])(app.AppComponent);

(...)

ng.router
    .RouteConfig([
      { path: '/', redirectTo: ['PlanInfo'] },
      { path: '/planInfo', component: app.planInfoComponent, name: 'PlanInfo', useAsDefault: true  }/*,
      { path: '/coverage', component: app.CoverageComponent, name: 'Coverage'  },
      { path: '/nonelective', component: app.nonElectiveComponent, name: 'NonElective'  },
      { path: '/loans', component: app.loansComponent, name: 'Loans'  },
      { path: '/enrollment', component: app.enrollmentComponent, name: 'Enrollment'  }*/
    ])(app.planComponent);

作为参考,您可以看看这个 plunkr:https://plnkr.co/edit/w61Ecbmuj7EfDnsYEHOS?p=info