【问题标题】:Angular2 typescript error initializers are not allowed in ambient contexts在环境上下文中不允许使用 Angular2 打字稿错误初始化程序
【发布时间】:2026-01-21 03:00:02
【问题描述】:

我正在使用 angular 和 typescript 版本 2.7.1。我收到以下错误:

“环境上下文中不允许使用初始化程序。
'any' 仅指一种类型,但在这里用作值。
模块 D/Mean/CRUD/project/AngularApp/node_modules/rxjs/operators/index 没有导出成员 'toPromise'。

【问题讨论】:

  • 请向我们展示您的代码!
  • 显示错误所在的employee.component.ts
  • 声明 var M = any;
  • declare var M = any; @Component({ selector: 'app-employee', templateUrl: './employee.component.html', styleUrls: ['./employee.component.css'], providers: [EmployeeService] }) export class EmployeeComponent implements OnInit {
  • import { map } from 'rxjs/operators'; import { toPromise } from 'rxjs/operators';

标签: angular typescript


【解决方案1】:

我终于找到了问题所在。

声明变量的语法是个问题,我使用了“=”而不是“:”

我改变了从

导入“toPromise”的方法

将 { toPromise } 从 'rxjs/operators' 导入到

导入'rxjs/add/operator/toPromise';

这两个修复了我的错误。

【讨论】: