Angular 2 Routing in plain Javascript (No Typescript)

后端 未结 3 1572
北海茫月
北海茫月 2021-01-13 10:31

So I have been battling to get the router working in Angular2 without using Typescript. I can\'t seem to find any examples, other than some typescript compiled javascript th

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-13 11:16

    Another example inspired of the Angular2 "Tour of Heroes" tutorial (you can find the full tutorial in plain javascript here: https://programming.sereale.fr/2016/03/22/rails-and-angular2-react-the-tour-of-heroes/):

    //= require directives/dashboard-component
    //= require directives/heroes-component
    //= require directives/hero-detail-component
    //= require services/heroes-service
    
    var MyApp = ng.core.Component({
        selector: 'my-app',
        directives: [ng.router.ROUTER_DIRECTIVES],
        providers: [ng.router.ROUTER_PROVIDERS, ng.http.HTTP_PROVIDERS, HeroService], // ng.http.HTTP_PROVIDERS enables to use http and get the list from the server
        template: "

    {{title}}

    \ \ " }).Class({ constructor: [ ng.router.Router, function(router) { router.config([ { path: '/dashboard', name: 'Dashboard', component: DashboardComponent, useAsDefault: true }, { path: '/heroes-list', name: 'Heroes', component: HeroesComponent }, { path: '/detail/:id', name: 'HeroDetail', component: HeroDetailComponent } ]); this.title = 'Tour of Heroes'; }] });

提交回复
热议问题