简体   繁体   English

如何在angular 6的单个组件中动态引用2个模板html文件(移动和桌面)?

[英]How to refer 2 template html file (mobile and desktop) dynamically in single component in angular 6?

My approach is as below. 我的方法如下。

Folder structure 资料夹结构

mobile-view.component.html mobile-view.component.html

<p> this is mobile view</p>

desktop-view.component.html desktop-view.component.html

<p> this is desktop view</p>

mobile.component.ts mobile.component.ts

import { BaseComponent } from './base.component';

@Component({
    selector: 'app-mobile-view',
    templateUrl: './mobile-view.component.html'
})

export class MobileComponent extends BaseComponent { }

desktop.component.ts desktop.component.ts

import { BaseComponent } from './base.component';

@Component({
    selector: 'app-desktop-view',
    templateUrl: './desktop-view.component.html'
})

export class DesktopComponent extends BaseComponent { }

base.component.ts base.component.ts

@Component({
   selector: 'app-root',
   template: `<app-mobile-view *ngIf="isMobileView"></app-mobile-view>

    <app-desktop-view *ngIf="!isMobileView"></app-desktop-view>`
})
export class BaseComponent implements {

   isMobileView: boolean;

   constructor(){
        if (navigator.userAgent &&
           (navigator.userAgent.match(/Android/i) ||
           navigator.userAgent.match(/webOS/i) ||
           navigator.userAgent.match(/iPhone/i) ||
           navigator.userAgent.match(/BlackBerry/i) ||
           navigator.userAgent.match(/Windows Phone/i))) {

           this.isMobileView = true;
        } else {
           this.isMobileView = false;
        }
   }

   ngOnInit() {
    // code
   }

   // various methods
}

In this approach my main logic and all binding variables reside in base.component.ts 在这种方法中,我的主要逻辑和所有绑定变量都位于base.component.ts中

Mobile component and Desktop component extends Base component to access all methods and variables 移动组件和桌面组件扩展了基本组件以访问所有方法和变量

Note:- This is demo code just for understanding what i have tried. 注意:-这是演示代码,仅用于了解我的尝试。

Requirement : 要求:

  1. AOT build is must. 必须构建AOT。
  2. Need any better approach or standard practice for achieving this 需要任何更好的方法或标准实践来实现这一目标

  3. Need a way to set template dynamically based on device from which it is request. 需要一种根据请求设备动态设置模板的方法。 Refer base.component.html constructor for code of getting userAgent(provide info of device from which request is comming) which i am using currently. 请参阅base.component.html构造函数以获取获取我当前正在使用的userAgent(请求来自的设备的提供信息)的代码。

Previous method used:- 以前使用的方法:

main.component.html main.component.html

@Component({
   selector: 'app-root',
   template: function () {
     if (isMobileUser()) {
            return require('./mobile-view.component.html');
       } else {
            return require('./desktop-view.component.html');
       }
     }(),
    styleUrls: ['./main.component.scss']
})

export class MainComponent {
}
  1. In this approach AOT build is failing 在这种方法中,AOT构建失败
  2. function call in component decorator is deprecated 组件装饰器中的函数调用已弃用

You can follow this approach which is simpler when compared to above one. 与上面的方法相比,您可以采用这种方法更简单。 using ngContainer with ngTemplateoutlet, you can inject the template into the container based on condition. 结合使用ngContainer和ngTemplateoutlet,可以根据条件将模板注入到容器中。

@Component({
    selector: 'app-mobiledesktop-view',
    templateUrl: './mobiledesktop-view.component.html'
})

export class MobileDesktopComponent { }


**Template:**

<ng-template #mobileView>
  This is mobile view
</ng-template>

<ng-template #desktopView>
  This is desktop view
</ng-template>

<ng-container *ngTemplateOutlet="isDesktop ? desktopView : mobileView">

</ng-container>

Using Dynamic Component: 使用动态组件:

@Component({
    selector: 'app-desktop-view',
    template: 'This is desktop view'
})

export class AppDesktopComponent { }

@Component({
    selector: 'app-mobile-view',
    template: 'This is mobile view'
})

export class AppMobileComponent { }

@Component({
    selector: 'app-container-view',
    template: ' <ng-container #messagecontainer></ng-container>'
})

export class AppContainerComponent { 
     private componentRef;
     @ViewChild('messagecontainer', { read: ViewContainerRef }) entry: ViewContainerRef;
     constructor(private resolver: ComponentFactoryResolver) { }

     ngAfterViewInit(){
        const component = (isDesktop) ? AppDesktopComponent : AppMobileComponent;
        const factory = this.resolver.resolveComponentFactory(component);
        this.componentRef = this.entry.createComponent(factory);
        //this.componentRef.instance.data = appData;
     }

     ngOnDestroy() {
        this.componentRef.destroy();
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在组件Angular 2中分离移动和桌面的模板和样式? - How to separate template and style for mobile and desktop in component Angular 2? 如何在Angular2中的模板html上动态创建数组组件? - How to rendered dynamically created array component on template html in Angular2? 在html模板angular 2中动态设置组件的属性 - Dynamically set attribute on component in html template angular 2 如何在 angular 9 中为同一个组件实现两个 html 文件? 一种用于移动尺寸,一种用于台式机? - How to implement two html files for the same component in angular 9? One for mobile sized and one for desktop? 如何动态更改Angular组件中使用的html文件? - How to dynamically change html file being used in component in Angular? 如何在 Angular 组件的模板中加载 HTML 文件? - How do load an HTML file in the template of a component in Angular? 如何在 Angular9 的组件中使用外部 html 文件作为模板? - How to use external html file as template in a component of Angular9? 如何在Angular 2,Ionic 2的组件中动态更新模板 - How to dynamically update template in component of Angular 2, Ionic 2 如何在Angular 2组件中动态加载外部模板? - How to dynamically load external template in Angular 2 Component? 使用html文件作为角度2中组件的模板 - Using html file as template for component in angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM