简体   繁体   English

从.d.ts文件导入打字稿

[英]Typescript import from .d.ts file

I wrote a (at this point) simple components for angular2. 我为此编写了一个简单的angular2组件。

Here's my code so far: 到目前为止,这是我的代码:

import {Component, View} from 'angular2/angular2';

export module SkApp.Core{
  @Component({
    selector: 'sk-app-side-menu'
   })
  @View({
    template: `
        <p>Hello</p>
    `
  })
  export class SkAppSideMenu {
    menuActive: boolean;

    constructor(){

      this.menuActive = false;
    }


    showMenu(){

      this.menuActive = !this.menuActive;

    }
  }
}

Of course there's no real functionality at this point. 当然,目前还没有真正的功能。 But that's not the point right now. 但这不是现在的重点。

I also created my own build process with gulp. 我还使用gulp创建了自己的构建过程。 Here's the relevant part of it: 这是其中的相关部分:

gulp.task('script', ['clean'], function() {
    var tsResult = tsProject.src()
      .pipe(ts(tsProject));

      return merge([ 
       tsResult.dts.pipe(rename({dirname: ''}))
       .pipe(concat('sk-app.d.ts'))
       .pipe(gulp.dest('dist/')),
       tsResult.js
       .pipe(rename({dirname: ''}))
       .pipe(concat('sk-app.min.js'))
       .pipe(uglify())
       .pipe(gulp.dest('dist/'))

   ]);
});

At the end I get two files: sk-app.min.js and sk-app.d.ts 最后,我得到两个文件:sk-app.min.js和sk-app.d.ts

sk-app.min.js: sk-app.min.js:

var __decorate=this&&this.__decorate||function(t,e,n,r){if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)return Reflect.decorate(t,e,n,r);switch(arguments.length){case 2:return t.reduceRight(function(t,e){return e&&e(t)||t},e);case 3:return t.reduceRight(function(t,r){return void(r&&r(e,n))},void 0);case 4:return t.reduceRight(function(t,r){return r&&r(e,n,t)||t},r)}},__metadata=this&&this.__metadata||function(t,e){return"object"==typeof Reflect&&"function"==typeof Reflect.metadata?Reflect.metadata(t,e):void 0},angular2_1=require("angular2/angular2"),SkApp;!function(t){var e;!function(t){var e=function(){function t(){}return t.prototype.showMenu=function(t){},t=__decorate([angular2_1.Component({selector:"sk-app"}),angular2_1.View({template:'\n        <button (click)="showMenu()">click me!</button>\n\n        <ng-content></ng-content>\n      ',directives:[angular2_1.NgClass]}),__metadata("design:paramtypes",[])],t)}();t.SkApp=e}(e=t.Core||(t.Core={}))}(SkApp=exports.SkApp||(exports.SkApp={}));

sk-app.d.ts: sk-app.d.ts:

export declare module SkApp.Core {
    class SkApp {
        showMenu(x: string): void;
    }
}

Now I want to use this component within another angular2 project: 现在,我想在另一个angular2项目中使用此组件:

import {Component, View, bootstrap} from 'angular2/angular2';
import {SkApp} from "SkApp/Core";

@Component({
    selector: 'my-app'
})
@View({
    template: '<sk-app></sk-app>',
    directives: [SkApp]
})
class MyAppComponent {

}

bootstrap(MyAppComponent);

But when I try to compile this project to js the typescript compiler gives me this message: 但是,当我尝试将该项目编译为js时,打字稿编译器会向我显示以下消息:

error TS2307: Cannot find module 'SkApp/Core' 错误TS2307:找不到模块'SkApp / Core'

When I change the module-definition within my sk-app.d.ts file to: 当我将sk-app.d.ts文件中的模块定义更改为:

declare module "SkApp/Core" {

it works fine. 它工作正常。 But I want to have my definition files to be generated by gulp without having to change the module declaration myself. 但是我想让gulp生成我的定义文件,而不必自己更改模块声明。

Any help with this would be appreciated. 任何帮助,将不胜感激。

(Of course I am aware, that angular2 is still in alpha state) (我当然知道,angular2仍处于Alpha状态)

export module SkApp.Core{ I think it should be: 导出模块SkApp.Core {我认为应该是:

 module SkApp.Core {...

Since for what I know, Typescript doesn't have module level visibility modifiers 因为据我所知,Typescript没有模块级别的可见性修饰符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM