簡體   English   中英

Angular2路由問題

[英]Angular2 routing issue

我正在嘗試運行使用深度路由的Angular2 beta示例,但route.navigate()不起作用,如果我使用routeLink,則它可以在同一路由上工作

@Component({
    templateUrl: './app/templates/main-menu.component.html',
    directives: [RouterOutlet, RouterLink],
    //providers: [ROUTER_PROVIDERS]
})

@RouteConfig([

        { path: '/operations/...', name: 'Operations', component: OperationsComponent, useAsDefault: true },
        { path: '/quotes', name: 'Quotes', component: QuotesComponent, useAsDefault: false},
        { path: '/customers', name: 'Customers', component: CustomersComponent, useAsDefault: false },
        { path: '/contacts', name: 'Contacts', component: ContactsComponent, useAsDefault: false },
        { path: '/maintenance', name: 'Maintenance', component: MaintenanceComponent, useAsDefault: false }//

])
export class MainMenuComponent implements OnInit{
    constructor(
        private _router: Router,
        private _routerParams: RouteParams

    ) { }

    menus: MenuItem[];
    selectedMenuItem: MenuItem;

    onSelect(item: MenuItem) {
        this.selectedMenuItem = item;
        this._router.navigate[item.name];


    }

export class MenuItem {
    constructor(public id: number, public name: string) { }
}

    export var Menus: MenuItem[] = [
        new MenuItem(1, 'Operations'),
        new MenuItem(2, 'Quotes'),
        new MenuItem(3, 'Customers'),
        new MenuItem(4, 'Contacts'),
        new MenuItem(5, 'Maintenance')
    ]

在html模板中,如果我使用routeLink,它將起作用:

<h1 style="margin-left:15px">Main Menu</h1>

<table>
    <tr>
        <td>
            <ul style="height:1000px">
                <li class="MainMenuItem" *ngFor="#item of menus" (click)="onSelect(item)" [class.MainMenuSelectedItem]="item === selectedMenuItem">
                    <div>
                        <div style="margin-left:15px; height:15px; width:15px; display:inline-block;
                    vertical-align:middle; margin-top:-2px;
                    background : linear-gradient(0deg, rgba(168, 213, 237, 1) 0%, rgba(164, 211, 236, 1) 15.72%, rgba(151, 204, 233, 1) 32.68%, rgba(129, 194, 227, 1) 50.24%, rgba(99, 178, 219, 1) 68.19%, rgba(60, 159, 210, 1) 86.27%, rgba(26, 142, 201, 1) 100%);
                    box-shadow : 1px 1px 0px rgba(255, 255, 255, 1);">
                        </div>


                        <a [routerLink]="[item.name]">{{item.name}}</a>
                    </div>
                </li>
            </ul> 
        </td>
        <td>
            <div  style="width:1450px;height:1000px;background-color:bisque;vertical-align: top;margin-left:-95px"> 
                <router-outlet></router-outlet>
            </div>
         </td>
    </tr>
</table>

提前致謝。

我認為您以錯誤的方式使用了navigate方法。 這是一個示例:

this._router.navigate( [ 'Details', { /* parameters */ }] );

Details@RouteConfig定義的路由namename屬性) @RouteConfig

所以我認為您的情況應該是這樣的:

this._router.navigate([item.name]);

關於router-link指令,語法如下(來自Angular文檔):

<a [routerLink]="['./User']">link to user component</a>

就您而言,我認為item.name不是您的路線路徑,而是名稱...

希望對您有幫助,蒂埃里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM