我有一个常量文件constants.ts:
export const C0NST = "constant";
Run Code Online (Sandbox Code Playgroud)
我在some.service.ts服务中访问它,如下所示:
import { C0NST } from './constants';
console.log(C0NST); // "constant"
Run Code Online (Sandbox Code Playgroud)
但是,当我在组件模板中访问它时:
some.component.ts:
import { C0NST } from './constants';
Run Code Online (Sandbox Code Playgroud)
some.component.html:
{{ C0NST }} <!-- Outputs nothing -->
Run Code Online (Sandbox Code Playgroud)
但是,在组件类中定义成员有效:
some.component.ts
public const constant = C0NST;
Run Code Online (Sandbox Code Playgroud)
some.component.html
{{ constant }} <!-- constant -->
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我能够直接在服务类中访问导入的常量,但不能在组件模板中访问,即使我在组件类中导入它.
我在表单组上有此条件:
if((age>17 && (this.frType=="Infant"))
|| (age>40 && this.frType=="Grandchild")
|| (age<=5 &&
(this.frType!="Child"
|| this.frType!="Infant"
|| this.frType!="Grandchild" || this.frType!="Cousin")))
Run Code Online (Sandbox Code Playgroud)
它包含3个主要条件:
infantgrandchildchild,infant,grandchild或cousin。如果这些条件之一为真,我将发送一条错误消息。
我收到的错误是:
[ts]由于类型'“ Child”'和'“ Infant”'没有重叠,因此此条件将始终返回'true'。[2367]
在这部分if条件上:
|| this.frType!="Infant" || this.frType!="Grandchild" || this.frType!="Cousin")))
Run Code Online (Sandbox Code Playgroud)
我在其他组件中使用了确切的条件,并且没有显示错误。
if((age>17 && (this.family_relation_type=="Infant"))
|| (age>40 && this.family_relation_type=="Grandchild")
|| (age<=5 &&
(this.family_relation_type!="Child" ||
this.family_relation_type!="Infant" ||
this.family_relation_type!="Grandchild" ||
this.family_relation_type!="Cousin")))
Run Code Online (Sandbox Code Playgroud)
这是我在这两个部分中计算年龄的方式:
let timeDiff = Math.abs(Date.now() - this.formGroup.controls['dob'].value);
let age = Math.floor((timeDiff / (1000 * 3600 * …Run Code Online (Sandbox Code Playgroud)