Javascript Angular 2-如何使用新的Angular 2.0.0-rc.1路由器

Javascript Angular 2-如何使用新的Angular 2.0.0-rc.1路由器,javascript,angular,angular2-routing,Javascript,Angular,Angular2 Routing,我开始写一个新的angular 2项目,我发现我安装了2个angular路由器: “@angular/router”:“2.0.0-rc.1” “@angular/router已弃用”:“2.0.0-rc.1” 我在angular网站上找不到如何使用新路由器。例如,我需要导入什么组件 因此,我的问题是: 我应该使用已弃用的路由器吗 关于如何使用新路由器,有什么好的文档吗 RC.1的更新 Angular2 RC.1的新路由器@angular/router已弃用。 Angular团队正致力于再次提供

我开始写一个新的angular 2项目,我发现我安装了2个angular路由器:

  • “@angular/router”:“2.0.0-rc.1”
  • “@angular/router已弃用”:“2.0.0-rc.1”
  • 我在angular网站上找不到如何使用新路由器。例如,我需要导入什么组件

    因此,我的问题是:

  • 我应该使用已弃用的
    路由器吗
  • 关于如何使用新路由器,有什么好的文档吗

  • RC.1的更新

    Angular2 RC.1的新路由器
    @angular/router
    已弃用。
    Angular团队正致力于再次提供新路由器。
    建议继续使用旧的
    @angular/router已弃用的
    路由器,直到新路由器可用为止

    原创

    新路由器的文档正在处理中。例如,见


    新的路由器有一些问题,但总体来说已经运行良好。如果您不只是想了解如何使用它,我至少会等待下一个Angular RC版本。一些早期采用者报告了一些问题,其中大多数问题可能很容易解决。

    这帮助我开始使用新路由器:

    编辑:以上链接暂时为空。。得益于tylerjgarland的缓存版本:

    我还发现来自ng conf的Misko Hevery的路由器谈话很有帮助:

    更新:看来RC1路由器正在被放弃? 也许这就是为什么文档消失而不是完成的原因

    更新2:RC2路由器现已发布:

    组件路由器处于alpha版本。这是推荐的Angular 2路由器,它取代了早期不推荐的beta和v2路由器

    新alpha路由器的
    package.json
    中的这一行:

    "@angular/router": "3.0.0-alpha.7",
    

    正如我在这里发现的:

    angular2的工作嵌套路由代码:“@angular/router”:“2.0.0-rc.1”,即新路由器如下所示:

    父路由:

    import {Component} from '@angular/core';
    import {Router,Routes,ROUTER_DIRECTIVES} from '@angular/router';
    
    import {Login} from '../login/login';
    import {Dashboard} from '../dashboard/dashboard';
    import {Admin} from '../admin/admin';
    let template = require('./app.html');
    
    @Component({
      selector: 'auth-app',
      template: template,
      directives: [ROUTER_DIRECTIVES],
    })
    
    @Routes([
      {path: '/login', component: Login},
      {path: '/dashboard', component: Dashboard},
      {path: '/admin', component: Admin }
    ])
    
    export class App{
      constructor(public router: Router) {
      }
    }
    
    子路径

    import { Component} from '@angular/core';
    import { CORE_DIRECTIVES } from '@angular/common';
    import { Router, ROUTER_DIRECTIVES ,Routes} from '@angular/router';
    
    import {AddUsrCat} from './addUsrCat/addUsrCat';
    import {AllUsr} from './allUsr/allUsr';
    declare var jQuery:JQueryStatic;
    let template = require('./admin.html');
    
    @Component({
      selector: 'admin',
      directives: [CORE_DIRECTIVES, ROUTER_DIRECTIVES],
      template: template
    })
    @Routes([
      {path: '/addUsrCat', component: AddUsrCat},
      {path: '/allUsr', component: AllUsr},
      {path: '*', component: AddUsrCat},
    ])
    
    export class Admin {
      constructor(public router: Router, public http: Http) {
      }
    }
    

    克隆此使用@angular/router的项目,即新路由

    正在进行的新angular 2路由器文档和开发工作。在此之前,您可以使用“@angular/router已弃用”


    @AkhileshKumar的建议很好,试试吧,我认为它涵盖了基本路由器的使用。

    这里是如何使用Angular 2路由器(RC1),与beta版(不推荐)路由器相比:

    • Routes
      替换
      RouteConfig
    • 在您的配置中有一个新的语法:

      {path:'/path',component:MyPathComponent}

      而不是:

      {path:'/path',name:'MyPath',component:MyPathComponent}

    • 现在使用routerLink是这样的:

      以下是新路由器的概述:

      这是一个正在工作的plunker:

      这里是另一个您可以尝试的资源(Angular RC.1):

      代码如下:

      我建议您使用Angular CLI创建新路由:

      例如:

      使用
      @angular/router不推荐使用的
      不是新的Angular2 RC.1路由器。他问的是如何使用新路由器,而不是旧路由器。但是现在有了新的routeNote,正如在下面文章的评论中提到的,这个“新的”RC.1路由器已经被弃用并重新编写。(不过我对这句话的来源很感兴趣)@netmikey有一句话指向。我还没有找到导致这一点的讨论。最后,有一些官方解释:“这一章正在进行中”。我猜文档在某些时候会有帮助,但现在不行。@Steve,谢谢你的确认。我清楚地记得几周前使用过这个页面。这是一个缓存视图。它也被弃用了,这里有一个最新的升级指南:上面这篇文章谈论的angular路由器的女巫版本?2.0.0版本的RC1或3.0.0版本的RC1?版本为2.0.0
      
      routerOnActivate(curr: RouteSegment): void {
                 curr.getParam('id');
             }
      
      import {ROUTER_DIRECTIVES, Router, Routes} from "@angular/router";
      
      @Injectable()
      @Component({
          selector: "my-app",
          templateUrl: `<a [routerLink]="['/component1']">Click to go to component 1</a>`,
          directives: [ROUTER_DIRECTIVES]
      })
      @Routes([
          {path: "/component1", component: Component1},
          {path: "/component2", component: Component2}
      ])
      export class AppComponent {
          constructor(private _router: Router) {
              //If you want to use Router in your component (for navigation etc), you can inject it like this
          }
      }