angular 2 typescript无法在环境上下文中声明实现

poa*_*oas 12 directive typescript angular

我是打字稿的新手,我正在尝试为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()
  }
}
Run Code Online (Sandbox Code Playgroud)

bas*_*rat 24

无法在环境上下文中声明实现

您最有可能将您的文件命名为foo.d.ts而不是foo.ts.这标志着它作为一个声明文件(更多关于https://basarat.gitbooks.io/typescript/content/docs/types/ambient/d.ts.html),你不能把逻辑放在这些中,因为你宣布了什么逻辑存在于别处.

  • 不用担心 我也这样https://en.wikipedia.org/wiki/Impostor_syndrome (2认同)

H D*_*Dog 9

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

它是在我复制/粘贴一些带有export declare class. 我注意到当我把它改成export class这个错误就消失了。

改变这个:

export declare class Foo {
}
Run Code Online (Sandbox Code Playgroud)

对此:

export class Foo {
}
Run Code Online (Sandbox Code Playgroud)


小智 8

我通过执行以下操作修复它:npm install rxjs