我是typescript新手,我正在try 为angular 2指令创建一个函数.

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

该消息适用于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()
  }
}

推荐答案

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

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

Typescript相关问答推荐

如何获取嵌套对象属性的正确类型?

Webpack说你可能需要在Reactjs项目中增加一个加载器''

在映射类型中用作索引时,TypeScrip泛型类型参数无法正常工作

类型{}上不存在属性&STORE&A;-MobX&A;REACT&A;TYPE脚本

不推荐使用Marker类-Google map API警告

如何创建一家Reduxstore ?

React router 6(数据路由)使用数据重定向

专用路由组件不能从挂钩推断类型

RXJS将对键盘/鼠标点击组合做出react

从非文字数组(object.keys和array.map)派生联合类型

为什么TypeScrip不能解析对象析构中的赋值?

在正常函数内部调用异步函数

基于未定义属性的条件属性

设置不允许相同类型的多个参数的线性规则

在TypeScript中,如何通过一系列过滤器缩小类型?

如何键入';v型';

Typescript 从泛型推断类型

在Typescript 无法正常工作的 Three.js 中更新动画中的 GLSL 统一变量

元组解释

元素隐式具有any类型,因为string类型的表达式不能用于索引类型{Categories: Element;管理员:元素; }'