JSPM + TypeScript + PhotoSwipe:如何成功导入和使用库?

时间:2016-03-22 18:32:19

标签: typescript jspm photoswipe

我正在尝试在我的JSPM和TypeScript项目中使用PhotoSwipe库但没有成功(我在这里流血......)。

使用DefinitelyTyped的PhotoSwipe修改后的版本定义文件(原版没有用 - 得到“PhotoSwipe未定义”),我想出了这个:

declare var PhotoSwipe: PhotoSwipe.IPhotoSwipeStatic;
declare var PhotoSwipeUI_Default: PhotoSwipeUI_Default.IPhotoSwipeUI_DefaultStatic;

declare module PhotoSwipe {
    ...
    interface IPhotoSwipeStatic {

        new <T extends Options> (pswpElement: HTMLElement,
            uiConstructor: (new (pswp: PhotoSwipeInstance<T>, framework: UIFramework) => UI<T>) | boolean,
            items: PhotoSwipe.Item[],
            options: T): PhotoSwipeInstance<T>;
    }
}

declare class PhotoSwipeInstance<T extends PhotoSwipe.Options> {
    ...
}


declare module PhotoSwipeUI_Default {
    ...
    interface IPhotoSwipeUI_DefaultStatic {

        new (pswp: PhotoSwipeInstance<Options>, framework: PhotoSwipe.UIFramework): PhotoSwipeUI_DefaultInstance;
    }
}

declare class PhotoSwipeUI_DefaultInstance implements PhotoSwipe.UI<PhotoSwipeUI_Default.Options> {
    ...
}

尝试导入它,我似乎无法想象如何创建PhotoSwipe的实例:

const photoSwipe = new PhotoSwipe(pswpElement, PhotoSwipe.PhotoSwipeUI, items, options);

1)

declare module "photoswipe" {
    export = { PhotoSwipe, PhotoSwipeUI_Default };
}

import "photoswipe"; =&gt;我得到 ReferenceError:未定义PhotoSwipe

2)

declare module "photoswipe" {

    export var PhotoSwipe: PhotoSwipe.IPhotoSwipeStatic;
    export var PhotoSwipeUI_Default: PhotoSwipeUI_Default.IPhotoSwipeUI_DefaultStatic;
}

import { PhotoSwipe, PhotoSwipeUI_Default } from "photoswipe"; =&gt;我得到 TypeError:photoswipe_1.PhotoSwipe不是构造函数

  

任何人?

2 个答案:

答案 0 :(得分:0)

我知道它已经晚了,但是这里的解决方案:

require([ 
        'path/to/photoswipe.js', 
        'path/to/photoswipe-ui-default.js' 
    ], function( PhotoSwipe, PhotoSwipeUI_Default ) {

    //      var gallery = new PhotoSwipe( someElement, PhotoSwipeUI_Default ...
    //      gallery.init() 
    //      ...

});

来源 - http://photoswipe.com/documentation/getting-started.html

也不要忘记在以前导入某些文件:

import 'photoswipe/dist/photoswipe.css';
import 'photoswipe/dist/default-skin/default-skin.css';

我试图通过TypeScript方式使用全局类型并简单地执行此操作:

import { PhotoSwipe, PhotoSwipeUI_Default } from 'photoswipe';

但它没有用。我遇到了和你一样的错误。

祝你好运!

PS - 是的,我用webpack测试了它,但没有测试JSPM。

答案 1 :(得分:0)

npm install photoswipe
npm install --save @types/photoswipe

从npm安装photoswipe及其类型定义。

import photoSwipe from 'photoswipe'

然后你可以使用上面的import语句来使用它。

希望这会有所帮助。