2015-12-26 147 views
3

我正在嘗試使用ES5實現Angular 2路由。angular 2 es5組件路由

以下是appComponent

(function(app) { 
    app.AppComponent =function() {}; 
    app.AppComponent = ng.core 
    .Component({ 
     selector: 'my-app' 
    }). 
    View({ 
     templateUrl: 'app.html', 
     directives: [ng.router.ROUTER_DIRECTIVES] 
    }) 
    .Class({ 
     constructor: function() { 
      this.name = "pankaj Badukale"; 
     } 
    }); 

    **ng.router.RouteConfig([ 
     { 
      path: "login", 
      component: app.LoginComponent, 
      as: "login" 
     } 
    ]);** 

})(window.app || (window.app = {})); 

app.html

<h1> 
    {{name}} 
    <a [routerLink]="['/login']">Home</a> 
    <router-outlet></router-outlet> 
</h1> 

我想知道我們如何能夠配置在組件路由。

我已經搜索很多,但人們只是定義了查看,組件,類。

有沒有人有想法?

+0

請有看看這個解決方案。它可以幫助。 http://stackoverflow.com/questions/34604160/angular-2-routing-in-es5 – user1894683

+0

對不起,但隨着角2語法的Beta.0發生了變化,意味着一些高級語法來了。所以我期待着答覆。目前在lib中查找它................ –

回答

1

組件和RouteConfig都是裝飾,你可以寫在角2(測試版)裝飾像

app.AppComponent = ng.core.Component(...)(app.AppComponent); 
app.AppComponent = ng.router.RouteConfig(...)(app.AppComponent); 


這裏是你的工作副本...

(function(app) { 
    app.AppComponent =function() {}; 
    app.AppComponent = ng.core 
    .Component({ 
     selector: 'my-app' 
    }). 
    View({ 
     templateUrl: 'app.html', 
     directives: [ng.router.ROUTER_DIRECTIVES] 
    }) 
    .Class({ 
     constructor: function() { 
      this.name = "pankaj Badukale"; 
     } 
    }); 

    app.AppComponent = ng.router.RouteConfig([ 
     { 
      path: "login", 
      component: app.LoginComponent, 
      as: "login" 
     } 
    ])(app.AppComponent); 

})(window.app || (window.app = {}));