简体   繁体   English

如果打字稿中出现错误,则vscode中的angular 6项目

[英]if else error in typescript , angular 6 project in vscode

I am new to typescript and I am trying to build a file with constants in a angular 6 ionic/cordova project. 我是TypeScript的新手,正在尝试在angular 6 ionic / cordova项目中使用常量创建文件。 I created a service file through the angular cli with ng generate service appboot 我通过ng cli通过angular cli创建了一个服务文件, 生成service appboot

I want to create a simple if else ,I've searched and nothing should be wrong with my if else but I get a vscode error saying I am missing a ",". 我想创建一个简单的if if,我已经搜索过了,如果if else没什么问题,但是我收到一个vscode错误,说我缺少“,”。 And I also get an error when I run ionic serve. 当我运行离子服务时,我也会得到一个错误。 The error only appears when I try to type the else 仅当我尝试键入else时才会出现错误

In my appboot.service.ts I have: 在我的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 {}

Statements can't appear randomly inside a class body, they need to appear in method or constructor body. 语句不能随机出现在类主体中,它们需要出现在方法或构造函数主体中。 For example: 例如:

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 { }
    }
}

Also field access nees to be prefixed with this. 同样,字段访问也必须以此作为前缀this.

Your condition is outside the class. 您的情况超出了课堂。

Your condition could be in the constructor... : 您的条件可能在构造函数中……:

import { Injectable, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { environment } from '../environments/environment';

@Injectable({
    providedIn: 'root'
})
export class AppbootService {

    env: string;

    constructor() {
        if (this.env == "dev") {

        } else { }
    }
}

...it could also be in the ngOnInit life cycle... : ...也可能处于ngOnInit生命周期中...:

import { Injectable, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { environment } from '../environments/environment';

@Injectable({
    providedIn: 'root'
})
export class AppbootService implements OnInit {

    env: string;

    constructor() {}

    ngOnInit() {
        if (this.env == "dev") {

        } else { }
    }
}

...or simply in a new function. ...或者只是一个新功能。 :

import { Injectable, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { environment } from '../environments/environment';

@Injectable({
    providedIn: 'root'
})
export class AppbootService implements OnInit {

    env: string;

    constructor() {}

    ngOnInit() {}

    myfunction() {
        if (this.env == "dev") {

        } else { }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM