Angular 2 different views based on Desktop or Mobile

非 Y 不嫁゛ 提交于 2019-12-20 10:13:29

问题


I'm working on web site, that has slight different functionality based on version desktop/mobile.

I tried to apply it via @View, but it looks like this decorator is now deprecated. Please, advise me best practice, how to implement this feature in Angular 2.


回答1:


The ng2-responsive package should cover your needs: https://www.npmjs.com/package/ng2-responsive

I haven't used it extensively yet, but it seems to be doing a decent job.

@View was merged into @Component (ages ago). @Component should be the only decorator you need.




回答2:


@Component({
  selector: 'my-component',
  templateUrl: "./" + (window.screen.width > 900 ? 
                     "my-component.desktop.html" : 
                     "my-component.mobile.html"),
  styleUrls: ['./my-component.css']
})



回答3:


At the moment the best to replace @View decorator is using an *ngIf like this:

<div *ngIf="isMobile">
    modile stuff
</div>
<div *ngIf="!isMobile">
    desktop stuff
</div>


来源:https://stackoverflow.com/questions/39440694/angular-2-different-views-based-on-desktop-or-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!