Angular 2 - 路由使用AsDefault不起作用

时间:2016-05-23 10:32:31

标签: javascript angularjs typescript routing angular

我使用angular 2进行路由,使用节点进行本地托管。

当我对我的路线使用'useAsDefault:true'时,导航栏链接不再有效,当我希望它转到http://localhost/ http://localhost/home(空白页) >

我删除了导航栏正常工作的标志,我可以转到/ home路线但是我转到空白页

任何人都可以了解默认标志无法正常工作的原因吗?

App.Component.ts

@RouteConfig([
  { path: '/home', name: 'Home', component: HomeComponent /*, useAsDefault : true */},
  { path: '/articles', name: 'Posts', component: PostsComponent  },
  { path: '/detail/:id', name: 'PostDetail', component: PostDetailComponent },
  { path: '/login', name: 'Login', component: LoginComponent  },
])

App.Component.html

<ul class="topnav">
  <li><a [routerLink]="['Home']">Home</a></li>
  <li><a [routerLink]="['Posts']">Articles</a></li>
  <li><a href="#p">Publisher</a></li>
  <li><a [routerLink]="['Login']">Login</a></li>
</ul>

<router-outlet></router-outlet>

Main.ts

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';

import { HTTP_PROVIDERS } from '@angular/http';
import { ROUTER_PROVIDERS } from '@angular/router-deprecated';

bootstrap(AppComponent,  [ROUTER_PROVIDERS, HTTP_PROVIDERS]);

的index.html

<html>
  <head>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <title>Blog</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles/styles.css">

     <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>

    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>

  <!-- 3. Display the application -->
  <body>
    <blog-app>Blog Loading...</blog-app>
  </body>
</html>

3 个答案:

答案 0 :(得分:5)

只需为尝试访问的未定义的每条路线添加路线,并将其重定向到您的主路线:

@RouteConfig([
  { path: '/home', name: 'Home', component: HomeComponent },
  { path: '/articles', name: 'Posts', component: PostsComponent },
  { path: '/detail/:id', name: 'PostDetail', component: PostDetailComponent },
  { path: '/login', name: 'Login', component: LoginComponent },
  { path: '/**', redirectTo: ['Home'] }
])

答案 1 :(得分:2)

可能迟到了:

不推荐使用

useAsDefault,请使用:

const appRoutes: Routes = [
  { path: 'crisis-center', component: CrisisListComponent },
  { path: 'heroes',        component: HeroListComponent },
  { path: '',   redirectTo: '/heroes', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent }
];

官方文件:https://angular.io/docs/ts/latest/guide/router.html

答案 2 :(得分:0)

当你使用useasdefault时,它意味着你有一个父路线,并且在其中有子路线,即

localhost:8000/dashboard/dashboard
localhost:8000/dashboard/settings

两个网址路由将设置在父网址下,如下所示:

{path: '/dashboard/...', component:Dashboard}

然后在仪表板组件中,您现在创建仪表板根目录和设置路径作为子路径,如下所示:

{path: 'dashboard', component:DashboardAdmin, useAsDefault: True}
{path: 'settings', component:Settings}

在你的情况下,找出你需要封装的东西。例如,home, articledetail可以在一个父网址下面说:

{path: '/home/...' component: HomeDataComponent}
{ path: '/login', name: 'Login', component: LoginComponent  },

然后将三个网址配置移动到HomeDataComponent或您给它的名称:

@RouteConfig([
{ path: '/home', name: 'Home', component: HomeComponent , useAsDefault : true},
{ path: '/articles', name: 'Posts', component: PostsComponent  },
{ path: '/detail/:id', name: 'PostDetail', component: PostDetailComponent },
])

使用useAsDefault时,您需要首先显示父路线和子路线上的useAsDefault

检查this以获取详细说明。希望它有效。