简体   繁体   English

angular2子路线,无组件

[英]angular2 child route, no component

I'm trying to make an 'edit' url for my entity. 我正在尝试为我的实体创建一个“编辑” URL。 So, for example /my-site/entity/1 and /my-site/entity/1/edit . 因此,例如/my-site/entity/1/my-site/entity/1/edit The edit part is just used to toggle a state of the main/parent entity url. edit部分仅用于切换主/父实体URL的状态。

In angular 1, with the ui-router I could do something along the lines of 在角度1中,使用ui路由器,我可以按照

.state("home.entity.edit", {
    url: "/edit",
    reloadOnSearch: false
})

Ie this would be child state of home.entity . 也就是说,这将是home.entity子状态。 It would have no view or controller as the parent state would handle it. 它没有视图或控制器,因为父级状态会处理它。 Also on toggle to this state, nothing would actually happen in terms of redirect/controller execution. 同样在切换到此状态时,就重定向/控制器执行而言实际上什么也不会发生。

Is it actually possible to do this with the component router in angular2? 实际上,可以使用angular2中的组件路由器执行此操作吗?

Yes, this is possible using Router parameters. 是的,可以使用路由器参数来实现。 The edit parameter will be an optional parameter . edit参数将是一个可选参数 In your ngOnInit you will read the params from the request, and act accordingly. 在您的ngOnInit中,您将从请求中读取参数,并采取相应的措施。 It's slightly less automated than your example, but it gives more freedom as well. 与您的示例相比,它的自动化程度稍差一些,但是它也提供了更多的自由度。

For the moment, I've implemented it as follows 目前,我已经实现了如下

{
    path: 'view/:id',
    component: NotesViewComponent,
    children: [
        {
            path: '',
            component: null
        },
        {
            path: 'edit',
            canActivate: [EditModeActivator],
            component: null
        }
    ],
    data: {
        editMode: false
    }
}

where 哪里

@Injectable()
class EditModeActivator implements CanActivate {
    constructor() { }

    canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        if (next.url[0].path === "edit") {
            let data: any = next.parent.data;
            data.editMode = true;
        }

        return true
    } 
}

It is a hack yes, but will do for now! 是的,确实可以,但是现在就可以了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM