【问题标题】:Angular2 update URL on property changes (with browser history)Angular2 在属性更改时更新 URL(带有浏览器历史记录)
【发布时间】:2016-10-04 15:54:33
【问题描述】:

假设我有一个Component,它充当一种主要细节。我希望 URL 反映详细信息部分的更改,而无需重新加载整个 Component

这就是我所拥有的:

home.routing.ts

import { ModuleWithProviders } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";

import { HomeComponent } from "./home.component";
import { ItemListComponent } from "./item-list.component";

const homeRoutes: Routes = [
    {
        path: "home",
        component: HomeComponent,
        children: [
            {
                path: ":categoryId",
                component: HomeComponent
            }
        ]
    },
    {
        path: "home",
        component: HomeComponent
    }
];

export const homeRouting: ModuleWithProviders = RouterModule.forChild(homeRoutes);

home.component.ts

import { Component, OnInit } from "@angular/core";
import { Router, ActivatedRoute, Params } from "@angular/router";

...

@Component({
    template: `
            <div *ngFor="let category of categories">
                <span (click)="onSelect(category)">{{category.title}}</span>
            </div>
            <item-list></item-list>
    `,
})
export class HomeComponent implements OnInit {
    categories: Category[];
    selectedCategoryId: string;

    constructor(
        private route: ActivatedRoute,
        private router: Router) {
    }

    ngOnInit() {
        this.getCategories();

        // Update selectedCategoryId if it is set in the URL
        this.route.params.forEach((params: Params) => {
            this.selectedCategoryId = params["categoryId"];
        })
    }

    onSelect(category: Category) {
        this.selectedCategoryId = category.id;

        // This will trigger a change in the item-list component and the
        // items will be loaded based on the selected category. 
        // What can I do here to reflect the change of selected category
        // in the URL?
    }

    getCategories() {
        ...
        this.categories = categoriesFromRestAPI;
    }
}

item-list.component.ts

import { Component, Input } from "@angular/core";

...

@Component({
    selector: "item-list",
    template: `
        <div *ngIf="categoryId">
            <div *ngFor="let item of items">
                <span>{{item.text}}</span>
            </div>
        </div>
    `
})
export class ItemListComponent {
    private _categoryId: string;
    items: Item[];

    constructor() {
    }

    @Input()
    set categoryId(categoryId: string) {
        this._categoryId = categoryId;
    }

    get categoryId() {
        return this._categoryId;
    }

    getItems() {
        // Get items based on this._categoryId;
        items = itemsFromRestApi;
    }
}

当用户选择一个类别时,我如何在 URL 中反映这一点,并更新浏览器历史记录以使后退按钮起作用?可能吗?我问了一个类似的问题here,但改用路由器。我认为这两个选项都可以在不同的场景中使用,但我就是无法正确使用它们。

【问题讨论】:

    标签: angular angular2-routing angular2-components


    【解决方案1】:

    如果您使用 this.router.navigate(...)&lt;a [routerLink]="..." 导航到相同的路由,其中​​仅更改参数值,则组件实例将被重用并更新浏览器历史记录。

    【讨论】:

    • 好的。那么在这种情况下我应该使用/homehome?categoryId=123 之类的路由吗?
    • 这样参数就是查询参数。你想要的是路由参数。 /home/123 将匹配您的路由配置。
    • 但是如果没有选择任何类别,我怎么能涵盖最初的情况呢?那将是/home。然后使用选定的类别/home/12,这将是另一条路线。两条路由都应该加载相同的主/详细视图。
    • 您可以添加另一个具有空路径和相同组件children: [ { path: "", pathMatch: 'full', component: HomeComponent }, { path: ":categoryId", component: HomeComponent } ]的路由。在这种情况下,HomeComponent 将在您在这些路由之间切换时被销毁并重新创建(而不是仅在 categoryId 更改时)
    • 是的,就是这样!只要我将路由声明为子路由,组件就会被重用。谢谢!
    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 2014-01-23
    • 2015-11-15
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    相关资源
    最近更新 更多