【问题标题】:angular 2 typescript An implementation cannot be declared in ambient contextsangular 2 typescript 无法在环境上下文中声明实现
【发布时间】:2016-09-23 17:28:32
【问题描述】:

我是 typescript 的新手,我正在尝试为 angular 2 指令创建一个函数。 谁能用 n00bs 的语言解释一下当我使用 Gulp 编译时错误试图告诉我什么?

不能在环境上下文中声明实现

该消息适用于offset()toggler()

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({
  selector: 'offCanvas',
  inputs: ['target', 'toggle', 'placement', 'autohide', 'recalc', 'disableScrolling', 'modal', 'canvas', 'exclude'],
  host: {
    '(click)': 'Click()'
  }
})

export class OffCanvas {  
  @Input('target') target: string;
  @Input('canvas') canvas: string;
  @Input('state') state: string;
  @Input('exclude') exclude: string;
  @Input('placement') placement: string;
  @Input('toggle') toggle: boolean;
  @Input('autohide') autohide: boolean;
  @Input('recalc') recalc: boolean;
  @Input('disableScrolling') disableScrolling: boolean;
  @Input('modal') modal: boolean;

  public offset() {
    switch (this.placement) {
      case 'left':
      case 'right':  return (<HTMLElement>document.querySelector(this.target)).offsetWidth
      case 'top':
      case 'bottom': return (<HTMLElement>document.querySelector(this.target)).offsetHeight
    }
  }

  public toggler() {
    if (this.state === 'slide-in' || this.state === 'slide-out') return
    this[this.state === 'slid' ? 'hide' : 'show']()
  }

  Click() {
    this.toggler()
  }
}

【问题讨论】:

    标签: angular typescript directive


    【解决方案1】:

    不能在环境上下文中声明实现

    您很可能将文件命名为foo.d.ts 而不是foo.ts。这将其标记为声明文件(更多关于 https://basarat.gitbooks.io/typescript/content/docs/types/ambient/d.ts.html),您不能将 logic 放入其中,因为您要声明其他地方存在什么逻辑。

    【讨论】:

    • 有这个确切的问题,不小心编辑了一个编译文件...
    【解决方案2】:

    我通过以下方式修复了它: npm install rxjs

    【讨论】:

    • 谢谢。完成此操作后,我有一个干净的编译 - 不再有 rxjs“环境上下文”和“重复函数实现”错误。
    【解决方案3】:

    我遇到了这个相当不寻常的编译时 Typescript 错误error TS1183: An implementation cannot be declared in ambient contexts.

    它是在我复制/粘贴了一些包含export declare class 的源代码之后开始的。我注意到当我将其更改为 export class 时,此错误消失了。

    改变这个:

    export declare class Foo {
    }
    

    至此:

    export class Foo {
    }
    

    【讨论】:

      【解决方案4】:

      我认为这发生在我身上,因为我不小心编辑了 node_modules 中的一个文件。这为我解决了问题:

      rm -r node_modules
      npm install
      

      【讨论】:

        【解决方案5】:

        我遇到了同样的错误并通过键入来修复它:

        npm install --save typescript@latest
        

        package.json 现在有最新的 ts 版本,ts 编译器是 2.2.2。

        【讨论】:

        • 这给了我一个新错误,告诉我我需要回退到较低版本的打字稿,因为我的编译器与最新版本不兼容。将其退回到此错误后。所以感觉就像我在一个循环中。
        【解决方案6】:

        错误 TS1183:无法在环境上下文中声明实现。 解决方案:删除node模块并重新安装。

        【讨论】:

        • 有同样的问题。 Brand New Node Install 就在昨天 npm v 5.6.0 下架了 v8.11.2,问题仍然存在。
        • 这是个大问题,我也有同样的问题!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-22
        • 2020-05-24
        • 2020-08-16
        • 2020-08-16
        • 2021-07-09
        相关资源
        最近更新 更多