1

我是 Angular 4 的新手。不幸的是,我正在尝试使用路由器插座创建动态内容,但我无法使用它两次,因为我对平板电脑、手机和台式机有不同的场景。也许你们有什么建议?

我想我可以分配不同的网点, 但也许有更简单的方法?

我的代码:

<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>

谢谢您的帮助。

4

2 回答 2

0

解决了:

应用组件.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>
于 2017-10-03T08:52:05.647 回答
0

对于您的情况,不需要两个<router-outlet></router-outlet>

您可以通过使用 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>

但是如果你仍然想使用多个router-outlet

查看@Skeptor 在评论中建议的链接 http://onehungrymind.com/named-router-outlets-in-angular-2/

于 2017-10-03T04:30:03.067 回答