Angular 2 go from child to parent

How can I go from a child:

local / # / pages / configurations / from-devices / 1000

parent

local / # / pages / configurations / from devices

I tried:

back(){
    this.router.navigate("../", {relativeTo: this.route});
}

in a child component, but get redirected to the default site

my routing.ts

const routes: Routes = [
    { path: 'login', component: LoginComponent},
    { path: '', redirectTo: 'login', pathMatch: 'full' },
    {
        path: 'pages',
        canActivate: [AuthGuard],
        component: PagesComponent,
        children: [
            { path: '', redirectTo: 'devices'},
            { path: 'devices', component: DevicesComponent },
            { path: 'groups', component: GroupsComponent },
            {
                path: 'config',
                component: ConfigComponent,
                children: [
                    // general
                    { path: 'out-devices', component: ConfigOutDevicesComponent },
                    { path: 'out-devices/:id', component: ConfigOutDeviceDetailsComponent },

                ]
            }

        ]
    }
];
+3
source share
2 answers
this.router.navigate("../../out-devices", {relativeTo: this.route});
+3
source

It's not like you'll be redirected from the child to the parents, because both out-devices, and out-devices/:idare in the same affiliated group

children: [
   { path: 'out-devices', component: ConfigOutDevicesComponent },
   { path: 'out-devices/:id', component: ConfigOutDeviceDetailsComponent },
]

You can try this way, the child component is more reused, as it uses redirection in a more loosely coupled way.

back(){
   this.router.navigate("./", {relativeTo: this.route});
}
0

Source: https://habr.com/ru/post/1659696/


All Articles