2016-05-25 61 views
4

我是新来的打字稿,我正在尝试为angular 2指令创建一个函数。 任何人都可以用n00bs的语言解释,当我用Gulp编译时,错误是想告诉我什么?angular 2 typescript一个实现不能在环境中声明

的实现不能在周围环境

的信息适用于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() 
    } 
} 

回答

7

的实现不能在周围环境中声明

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

+0

我觉得很愚蠢...... – poashoas

+0

别担心。所以我的https://en.wikipedia.org/wiki/Impostor_syndrome – basarat

+1

大声笑,这个综合症让我做到了 – poashoas

1

我有同样的错误,并通过输入固定它:

npm install --save [email protected] 

的package.json已安装最新版本的TS和TS编译器是2.2.2。

相关问题