1
votes

I am quite new in Angular 4. I am trying to use router outlet to create dynamic content unfortunately, I am not able to use it twice, because I have different scenarios for tablets, mobiles and desktops. Maybe do you guys have some advise?

I think I can assign different outlets but maybe there is an easier way?

My code:

<div class="ui two column stackable grid">

    <app-navigation class="three wide column computer mobile only webso-navigation"></app-navigation>
    <app-navigation class="four wide column tablet only webso-navigation"></app-navigation>
    <div class="thirteen wide column computer mobile only webso-content">
        <router-outlet></router-outlet>
    </div>
    <div class="twelve wide column tablet only webso-content">
        <router-outlet></router-outlet>
    </div> 



</div>

Thank you for your help.

2
may be you can try 'named router outlet'. onehungrymind.com/named-router-outlets-in-angular-2 - Vamshi
use named routes - { path: 'compose', component: ComposeMessageComponent, outlet: 'popup' }, , <a [routerLink]="[{ outlets: { popup: ['compose'] } }]">Contact</a> , <router-outlet name="popup"></router-outlet> angular.io/guide/router - Gary

2 Answers

0
votes

For your case, there is no need of two <router-outlet></router-outlet>

You can simply achieve this by using ngClass

// In Component File
isMobile : boolean;
isTablet : boolean;

// In Template File
<div [ngClass]="{'thirteen wide column computer mobile only webso-content' : isMobile , 
                'twelve wide column tablet only webso-content' : isTablet }" >
        <router-outlet></router-outlet>
</div>

But if you still want to use multiple router-outlet ,

Checkout the link as @Skeptor suggested in comment http://onehungrymind.com/named-router-outlets-in-angular-2/

0
votes

Solved:

App.component.ts

export class AppComponent implements OnInit {


  title = 'app';
  innerWidth;

  isTablet : boolean = false;


  ngOnInit() {
    this.innerWidth = window.innerWidth;

    this.innerWidth <= 600 || this.innerWidth >= 1024 ? this.isTablet = false : this.isTablet = true;
  }

  @HostListener('window:resize', ['$event'])
  onResize(event) {
    this.innerWidth = window.innerWidth;

    this.innerWidth <= 600 || this.innerWidth >= 1024 ? this.isTablet = false : this.isTablet = true;

  }

App.component.html

<div [ngClass]="isTablet ? 'twelve wide column tablet only webso-content'
                                : 'thirteen wide column computer mobile only webso-content'">
        <router-outlet></router-outlet>
    </div>