错误 TS7027:在 Angular2 TypeScript 服务类中检测到无法访问的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40854634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:02:47  来源:igfitidea点击:

error TS7027: Unreachable code detected in Angular2 TypeScript Service class

node.jsangulartypescript

提问by Flea

I am following along with a PluralSight course and practicing writing a basic service in Angular2. I have the following service file:

我正在学习 PluralSight 课程并练习在 Angular2 中编写基本服务。我有以下服务文件:

customer.service.ts

客户服务.ts

import { Injectable } from '@angular/core';

@Injectable()
export class CustomerService {

    constructor() {}

    getCustomers() {
        return 
        [
            {id: 1, name: 'Ward'},
            {id: 2, name: 'Joe'},
            {id: 3, name: 'Bill'},
            {id: 4, name: 'Bob'},
            {id: 5, name: 'Levi'},
            {id: 6, name: 'Brian'},
            {id: 7, name: 'Susie'}
        ];
    }
}

When I start up the lite-server using npm startI am receiving the following error:

当我使用 lite-server 启动时,npm start我收到以下错误:

[email protected] start C:\Dev\my-proj
tsc && concurrently "tsc -w" "lite-server"

app/customer/customer.service.ts(10,3): error TS7027: Unreachable code detected.

When I comment out the return block, the lite-server starts fine, so it seems to be something in that return it does not like. Any help would be appreciated.

当我注释掉 return 块时,lite-server 开始正常,所以它似乎是它不喜欢的 return 中的某些东西。任何帮助,将不胜感激。

回答by Andreas J?gle

If you place your whole array in a new line after the return statement, then your method has two statements:

如果您将整个数组放在 return 语句之后的新行中,那么您的方法有两个语句:

  • return undefined
  • an array definition
  • 返回未定义
  • 数组定义

The error you see is a pretty helpful one. TypeScript tells you that there is code (the array definition) that could never be reached, because you are returning from the function already before the array line can be reached.

您看到的错误非常有用。TypeScript 告诉您有永远无法到达的代码(数组定义),因为您在到达数组行之前已经从函数返回。

So the solution is pretty simple. Move at least the opening bracket into the line of the return statement like return [. That's it.

所以解决方案非常简单。至少将左括号移动到 return 语句的行中,如return [. 而已。

回答by rtn

I tweaked it a bit. This should work:

我稍微调整了一下。这应该有效:

import {Injectable} from "@angular/core";

@Injectable()
export class CustomerService
{
    private data: Array<any>;

    constructor()
    {
        this.data = [
            {id: 1, name: 'Ward'},
            {id: 2, name: 'Joe'},
            {id: 3, name: 'Bill'},
            {id: 4, name: 'Bob'},
            {id: 5, name: 'Levi'},
            {id: 6, name: 'Brian'},
            {id: 7, name: 'Susie'}
        ];
    }

    getCustomers()
    {
        return this.data;
    }
}

or if you want the original the new array needs to start on the same line:

或者,如果您想要原始数组,则新数组需要在同一行开始:

import {Injectable} from "@angular/core";

@Injectable()
export class CustomerService
{

    constructor()
    {
    }

    getCustomers()
    {
        return [
            {id: 1, name: 'Ward'},
            {id: 2, name: 'Joe'},
            {id: 3, name: 'Bill'},
            {id: 4, name: 'Bob'},
            {id: 5, name: 'Levi'},
            {id: 6, name: 'Brian'},
            {id: 7, name: 'Susie'}
        ];
    }
}

回答by J C

It usually happens when you put some code after a return statement, or when you have a return inside of an if statement and you forget the else code.

当您在 return 语句之后放置一些代码时,或者当您在 if 语句中包含 return 并且忘记了 else 代码时,通常会发生这种情况。

...
return warning;
this.methodUnreacheable();

回答by Esref Atak

I resolved the problem for my case

我解决了我的案例的问题

I got the error because of unnecessary "break" expression:

由于不必要的“break”表达式,我得到了错误:

case "A": return false; break;

I removed the "break";

我删除了“休息”;

case "A": return false;

Resolved.

解决。

回答by Siddharth

Features in EcmaScript/Typescript makes it more like java or c#. So always use let or const keywords in your code.

EcmaScript/Typescript 中的特性使它更像 java 或 c#。所以总是在你的代码中使用 let 或 const 关键字。

let mood:Boolean=true;
if(mood){
console.log('true');
}
else{
console.log('false');
}