Angular2嵌套路由父级到子级ES5无法正常工作

时间:2016-03-01 13:37:09

标签: angularjs routing components angular

目前正在开发angular2 beta版本6,在这个嵌套路由中,如使用(EX:/ plan / ...)未来的父级到子级,不会在es5 JavaScript开发上工作,但在类型脚本开发中它工作正常完美

投掷错误:EXCEPTION: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'  }*/
            ]);
        }]
    });

1 个答案:

答案 0 :(得分: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