0

I want to pass objects from a parent object to a child object, and I want to use router functionality. Because router-outlet has no support for @input I want to use ngSwitch. But I definitly need the router suport for protecting routes and so on and route activation in my Typescript child object.

I have created a stackblitz: https://stackblitz.com/edit/angular-4j2g7b?file=src%2Fapp%2Fadmin%2Fadmin.component.html

I have predefined a ngSwitch but I want to have the router functionality in the child objects(template) like app-adminsub2.

This is my routing scheme:

const routes: Routes = [
  {
    path: 'admin',
    component: AdminComponent, children: [
      {
        path: 'adminsub1',
        component: AdminSub1Component
      },
      {
        path: 'adminsub2',
        component: AdminSub2Component
      }
    ]
  },
  {
    path: 'user',
    component: UserComponent,
  },
  {
    path: 'hello',
    component: HelloComponent,
  }
];

For giving you an overview.
This is the template code in the app-adminsub2 template and the Typescript code of this object.

<p>AdminSub2 works</p>

<h1>Suppliers</h1>
<p>The supplier for us is: {{ currentSupplier.description }} </p>
<p>Products:</p>
<ng-container *ngFor="let product of currentSupplier.products">
  <p>{{product}}</p>
</ng-container>

 @Input() currentSupplier : Supplier;

  constructor() {}

  ngOnInit () {

    console.log('Current supplier is: ' + this.currentSupplier.description)

  }

For other code see my Stackblitz with link above. Is it possible to add router functionality to a child object via ngSwitch or otherwise ?

Oh yes, important to mention, I need @Input and I can't use services.

Ben
  • 594
  • 1
  • 9
  • 24
  • why can't you use a service to communicate between components? That is a common way :) – AT82 Aug 22 '19 at 18:26
  • our software architect doesn't want services. – Ben Aug 22 '19 at 18:40
  • Well, really should rethink that or then start with something else than angular. Services are a big part and that is also what angular docs recommend, to communicate via services. I don't know if it is possible to do what you want. At least in no easy way. Maybe someone smarter knows. – AT82 Aug 22 '19 at 18:48
  • 1
    Your architect needs to be fired.. – MikeOne Aug 22 '19 at 18:59
  • I will tell them. :-). Services are allowed off course but not in this particular case. The code I have showed here is just displaying the principles. – Ben Aug 22 '19 at 19:31
  • No answers ? thats a pitty, Because the combination at input at output and ngOnChanges is a nice combination to use. Strange that Angular router-outlet doesn't support at input. Maybe it is because a router-outlet is a directive itself ? And a directive itself uses at input to communicatie with external objects ? I don't know its just a guess. – Ben Aug 24 '19 at 08:47

0 Answers0