Angular2路由匹配特定值

时间:2016-11-07 17:59:23

标签: angular angular2-routing

我正在尝试找到一种在url只有特定值时显示特定组件的方法。 基本上这就是现在看路由的方式:

const routes: Routes = [
  {
    path: 'users',
    component: MainComponent,
    children: [
      {
        path: ':id',
        component: DashboardComponent,
        children: [
          {
            path: '1',
            component: NeedToRenderThisComponent
          },
          {
            path: '',
            component: DashboardComponent
          },
          {
            path: '',
            outlet: 'sidebar',
            component: SidebarComponent
          }
        ]
      }
    ]
  }
];

所以我要找的是当用户点击或输入' / users / 1'时,用户会看到NeedToRenderThisComponent。对于所有其他值,将有DashboardComponent。

DashboardComponent包含两个路由器插座

<router-outlet name="sidebar"></router-outlet>
<router-outlet></router-outlet>

1 个答案:

答案 0 :(得分:0)

您可以使用using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MySql.Data.MySqlClient; public partial class _Default : System.Web.UI.Page { private MySqlConnection connection; private string server; private string database; private string uid; private string password; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) // if it is not a post back method, page renders for first time { connectdb(); } } private void connectdb() { server = "localhost"; database = "first_db"; uid = "root"; password = ""; string connectionString; connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; connection = new MySqlConnection(connectionString); try { connection.Open(); Console.ReadKey(); Response.Write("connection opened"); } catch (MySqlException ex) { //When handling errors, you can your application's response based //on the error number. //The two most common error numbers when connecting are as follows: //0: Cannot connect to server. //1045: Invalid user name and/or password. switch (ex.Number) { case 0: Response.Write("Cannot connect to server. Contact administrator"); break; case 1045: Response.Write("Invalid username/password, please try again"); break; } } } //end of connectdb } 路线,然后自行添加组件,如Angular 2 dynamic tabs with user-click chosen components所示。

AFAIK无法匹配值。在较旧的路由器中有正则表达式路由,但在当前的路由器中,这不是(还是?)实现的。

<强>更新

支持自定义路径匹配器已登陆,可能会在下次更新时提供 https://github.com/angular/angular/issues/12442