angular - 有没有办法在单个组件中控制 html pc 和 html mobile?
问题描述
我有以下代码
import { Component, OnInit } from '@angular/core';
import { mobile } from '../../../../environments/environment';
@Component({
selector: 'ctk-details-advisory',
templateUrl: (mobile) ? 'details-advisory.component.mobile.html' :
'details-advisory.component.html',
styleUrls: ['./details-advisory.component.scss'],
})
export class DetailsAdvisoryComponent implements OnInit {
// ...
这有助于我控制不同的 html、pc 和移动视图。问题是当我想执行单元测试时出现以下错误
未找到模块:错误:无法解析“(移动)?'details-advisory.component.mobile.html' : '/home/anonymous/criptoanalisis-0.2/frontend/src/app/pages/advisory/details-advisory' @ ./ src/app/pages/advisory/details-advisory/details-advisory.component.ts 23:22-121
对这个问题的一些解决方案或控制不同html的另一种方法会有很大帮助事实上我正在尝试解决这个问题我非常感谢任何建议
解决方案
使用单个html
( details-advisory.component.html
),而不是使用两个不同的模板(一个单独用于移动设备)。在component
代码中,定义一个类成员
public isMobile = mobile; // imported from the environment in the import section
然后,在template
html 代码中,您可以使用
<ng-container *ngIf="isMobile; else desktopView">
// all html related to mobile view
</ng-container>
<ng-container #desktopview>
// all html related to desktop view
</ng-container>
推荐阅读
- javascript - 如何在反应选择中更新选项
- c - 如何在 xv6 中添加延迟?
- reactjs - 我想将 flex-direction 属性设置为 Material UI Grid 标签
- podio - podio计算字段中@All函数的返回值
- docker - 无法启动服务 peer0.org1.firstproject.com:b'OCI 运行时创建失败:
- c# - 如何在 Visual Studio Windows 窗体设计中仅编辑选定文本的 FontStyle?
- vue.js - 设置加载叠加层的不透明度
- java - “null”应该是属性的有效值吗?
- amazon-web-services - 并行运行数千个容器的云计算服务
- tensorflow - Keras:BiLSTM 仅在 return_sequences=True 时有效