2016-03-01 67 views
1

当前正在使用angular2 beta版本6,在父级到子级使用(EX:/ plan/...)的嵌套路由中,未来不适用于es5 JavaScript开发,但在键入脚本开发它的工作完美Angular2嵌套路由父级到子级ES5不工作

投掷错误:EXCEPTION:链接“[”计划“]”不解析为终端指令。

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' }*/ 
      ]); 
     }] 
    }); 

回答

0

我认为你应该使用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