angular - 如果打字稿中出现其他错误,vscode中的角度6项目
问题描述
我是打字稿的新手,我正在尝试在 Angular 6 ionic/cordova 项目中构建一个带有常量的文件。我通过 angular cli 使用ng generate service appboot创建了一个服务文件
我想创建一个简单的 if else ,我已经搜索过,我的 if else 应该没有什么问题,但是我收到一个 vscode 错误,说我缺少一个“,”。当我运行 ionic serve 时,我也会遇到错误。该错误仅在我尝试键入 else 时出现
在我的 appboot.service.ts 我有:
import { Injectable, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { environment } from '../environments/environment';
@Injectable({
providedIn: 'root'
})
export class AppbootService {
env: string;
constructor() {
}
if(env == "dev")
}else {}
解决方案
语句不能随机出现在类体内,它们需要出现在方法或构造函数体内。例如:
import { Injectable, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { environment } from '../environments/environment';
@Injectable({
providedIn: 'root'
})
export class AppbootService {
env: string;
constructor() {
// Assuming env gets set somehow before the if
if (this.env == "dev") {
} else { }
}
}
还需要以字段访问为前缀this.
推荐阅读
- r - 如果 else 创建新的变量/列,但它没有出现在 R 数据框中
- selenium-webdriver - 获取 [错误] 鼠标:在 Mac 中使用 Sikuli 时无法使用(被阻止)
- r - 识别数据序列根据其他列 UserID 发生变化的情况
- javascript - JS 中的 Array(parameter).fill().map 是什么?
- typescript - 为接口做“导入类型”而不是“导入”有什么意义吗?
- python - 我在使用 matplotlib 和 numpy 在 python 中模拟物理方程时遇到了困难
- wordpress - 文件权限下缺少员工所有者
- django - non_field_errors": ["无效数据。需要一本字典,但得到了 InMemoryUploadedFile。"]
- python - Python中的“def”语法无效
- javascript - Javascript Screen WakeLock API 在 Windows 上不起作用