如何使用Aurelia/Typescript导入和使用PhotoSwipe? [英] How to import and use PhotoSwipe with Aurelia / Typescript?

查看:119
本文介绍了如何使用Aurelia/Typescript导入和使用PhotoSwipe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Aurelia项目中使用 PhotoSwipe ,但是找不到使它工作的方法./p>

在我的aurelio.json中的捆绑包中,我有:

{
    "name": "photoswipe",
    "path": "../node_modules/photoswipe/dist/",
    "main": "photoswipe.min",
    "resources": [
        "photoswipe-ui-default.min.js",
        "photoswipe.css",
        "default-skin/default-skin.css"
    ]
}

在我的package.json中,我有:

"@types/photoswipe": "^4.0.27",
"photoswipe": "^4.1.1"

在我的.ts模块中,

import PhotoSwipe from 'photoswipe';

我用于打开图库的代码(只是从入门文档)

imageClicked() { 

    var pswpElement = document.querySelectorAll('.pswp')[0];

    // build items array
    var items = [
        {
            src: 'https://placekitten.com/600/400',
            w: 600,
            h: 400
        },
        {
            src: 'https://placekitten.com/1200/900',
            w: 1200,
            h: 900
        }
    ];

    // define options (if needed)
    var options = {
        // optionName: 'option value'
        // for example:
        index: 0 // start at first slide
    };

    // Initializes and opens PhotoSwipe
    var gallery = new PhotoSwipe<PhotoSwipeUI_Default.Options>(pswpElement as HTMLElement, PhotoSwipeUI_Default, items, options);
    gallery.init();
}

aurelia项目构建正常,但是在运行时我收到此错误:

Uncaught ReferenceError: PhotoSwipeUI_Default is not defined

我尝试将导出添加到aurelia.json包

"exports": [ "PhotoSwipe", "PhotoSwipeUI_Default" ]

这没什么改变.

我尝试了各种导入语句:

import PhotoSwipeUI_Default from 'photoswipe'; // Now PhotoSwipeUI_Default is of type PhotoSwipe
import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default' // Module not found compile error
import PhotoSwipeUI_Default from 'npm:photoswipe@4.1.1/dist/photoswipe-ui-default.min.js'; // Cannot find module

我在黑暗中,解决这个问题的反复试验方法无济于事.

我想念什么?

解决方案

您需要更改配置.您将Aurelia指向缩小的文件,这显然导致了问题.让CLI处理JS文件的缩小.

{
  "name": "photoswipe",
  "path": "../node_modules/photoswipe/dist/",
  "main": "photoswipe",
  "resources": [
    "photoswipe-ui-default.js"
  ]
}

然后您可以使用

进行导入

import PhotoSwipe from 'photoswipe';
import PhotoSwipeUI_Default from 'photoswipe/photoswipe-ui-default';

此外,photoswipe css文件还会为某些内容加载图像文件. Aurelia CLI的当前局限性在于它打破了这种情况.此问题应在最终发布之前修复.但是现在,您需要使用link元素加载CSS.

  <!-- Core CSS file -->
  <link rel="stylesheet" href="node_modules/photoswipe/dist/photoswipe.css">

  <!-- Skin CSS file (styling of UI - buttons, caption, etc.)
     In the folder of skin CSS file there are also:
     - .png and .svg icons sprite, 
     - preloader.gif (for browsers that do not support CSS animations) -->
  <link rel="stylesheet" href="node_modules/photoswipe/dist/default-skin/default-skin.css">

请告诉我这是否适合您.

I am trying to use PhotoSwipe in an Aurelia project, but cannot find a way to make it work.

In my aurelio.json under bundles, I have:

{
    "name": "photoswipe",
    "path": "../node_modules/photoswipe/dist/",
    "main": "photoswipe.min",
    "resources": [
        "photoswipe-ui-default.min.js",
        "photoswipe.css",
        "default-skin/default-skin.css"
    ]
}

in my package.json, I have:

"@types/photoswipe": "^4.0.27",
"photoswipe": "^4.1.1"

in my .ts module, I have

import PhotoSwipe from 'photoswipe';

The code I use for opening the gallery (just copied from the getting started doc)

imageClicked() { 

    var pswpElement = document.querySelectorAll('.pswp')[0];

    // build items array
    var items = [
        {
            src: 'https://placekitten.com/600/400',
            w: 600,
            h: 400
        },
        {
            src: 'https://placekitten.com/1200/900',
            w: 1200,
            h: 900
        }
    ];

    // define options (if needed)
    var options = {
        // optionName: 'option value'
        // for example:
        index: 0 // start at first slide
    };

    // Initializes and opens PhotoSwipe
    var gallery = new PhotoSwipe<PhotoSwipeUI_Default.Options>(pswpElement as HTMLElement, PhotoSwipeUI_Default, items, options);
    gallery.init();
}

The aurelia project builds ok, but runtime i get this error:

Uncaught ReferenceError: PhotoSwipeUI_Default is not defined

I tried adding export to the aurelia.json bundle

"exports": [ "PhotoSwipe", "PhotoSwipeUI_Default" ]

This didn't change anything.

I tried various import statements:

import PhotoSwipeUI_Default from 'photoswipe'; // Now PhotoSwipeUI_Default is of type PhotoSwipe
import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default' // Module not found compile error
import PhotoSwipeUI_Default from 'npm:photoswipe@4.1.1/dist/photoswipe-ui-default.min.js'; // Cannot find module

I am in the dark, and my trial and error method of resolving this is going no where.

What am I missing?

解决方案

You need to change up your configuration. You're pointing Aurelia to the minified file which is causing issues apparently. Let the CLI handle minification of the JS files.

{
  "name": "photoswipe",
  "path": "../node_modules/photoswipe/dist/",
  "main": "photoswipe",
  "resources": [
    "photoswipe-ui-default.js"
  ]
}

You can then import using

import PhotoSwipe from 'photoswipe';
import PhotoSwipeUI_Default from 'photoswipe/photoswipe-ui-default';

Also, the photoswipe css files load image files for certain things. A current limitation of the Aurelia CLI is that it breaks this sort of thing. This should be fixed before final release. But for now, you'll need to load the CSS using link elements.

  <!-- Core CSS file -->
  <link rel="stylesheet" href="node_modules/photoswipe/dist/photoswipe.css">

  <!-- Skin CSS file (styling of UI - buttons, caption, etc.)
     In the folder of skin CSS file there are also:
     - .png and .svg icons sprite, 
     - preloader.gif (for browsers that do not support CSS animations) -->
  <link rel="stylesheet" href="node_modules/photoswipe/dist/default-skin/default-skin.css">

Please let me know if this works for you.

这篇关于如何使用Aurelia/Typescript导入和使用PhotoSwipe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆