首页 > 解决方案 > 如果打字稿中出现其他错误,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 {}

标签: angulartypescriptcordovavisual-studio-code

解决方案


语句不能随机出现在类体内,它们需要出现在方法或构造函数体内。例如:

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.


推荐阅读