【问题标题】:Using a normal JS librarary with Ionic2 + typescript使用带有 Ionic2 + typescript 的普通 JS 库
【发布时间】:2016-08-21 06:03:56
【问题描述】:

我正在尝试在我的 ionic2 + typescript 项目中使用 camanjs

我还查看了Ionic and Typings blog post by Mike,但它显示添加了一个已经在 Typings 中的库

然后我发现this blog post from josh on adding goole maps 使用CDN 方法。

到目前为止,我已经关注了他们两个,

通过 CDN 将 camanjs 添加到 index.html 文件中

#index.html
<script src="https://cdnjs.cloudflare.com/ajax/libs/camanjs/4.1.2/caman.full.js"></script>
<script src="cordova.js"></script>
...

下面是我的ts文件

#home.ts 
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';

declare var Camanjs: any;

@Component({
  templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
  constructor(public navCtrl: NavController) {
  }

  addFilter(){
     Camanjs("#image", function(){
       this.sinCity();
       this.render();
     })
  }
}

还有我的 html 文件。 (当用户单击我要应用过滤器的按钮时)

#home.html
<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <button (click)="addFilter()">Filter</button>
  <img id='image' src="https://d339b5nop2tkmp.cloudfront.net/uploads/pet_photos/2016/7/13/469479_e76aa_340x340_af1c8.jpg">
</ion-content>

但是当我点击addFilter() 时,我收到以下错误

browser_adapter.js:84 ReferenceError: Camanjs is not defined
    at HomePage.addFilter (home.ts:14)
    at DebugAppView._View_HomePage0._handle_click_13_0 (HomePage.template.js:201)
    at view.js:375
    at dom_renderer.js:254
    at dom_events.js:27
    at ZoneDelegate.invoke (zone.js:323)
    at Object.onInvoke (ng_zone_impl.js:53)
    at ZoneDelegate.invoke (zone.js:322)
    at Zone.runGuarded (zone.js:230)
    at NgZoneImpl.runInnerGuarded (ng_zone_impl.js:86)

但是我没有通过 IDE 或在编译时收到任何编译器错误????,任何帮助将不胜感激。

请注意my previous question 的扩展/更详细版本

【问题讨论】:

    标签: angular ionic2 camanjs


    【解决方案1】:

    不要使用Camanjs... 试试这个:

    Caman('#my-image', function () {
      // ...
    });
    

    因此,将 Camanjs 替换为 Caman 应该允许您调用该库上的方法。

    【讨论】:

    • 嘿@sebaferreras,感谢您的回复。它确实有效?。我更新了declare var Caman: any; 并开始将其用作Caman。所以据我了解,声明变量将停止打字稿抱怨?还有一件事要澄清(如果可能的话:)),如果我使用npm install,而不是CDN,我如何在.ts 文件中引用javascript 库?再次感谢:)
    • 很高兴我能帮忙 :) 是的,这样声明变量只是为了避免打字稿抱怨不知道那是什么,但在运行时没有任何影响。如果您使用 npm 添加它(我个人比 CDN 更喜欢这种方法),您需要在 node_modules(假设是 camanjs)中找到文件夹的名称,然后像 import * from 'camanjs'import { Caman } from 'camanjs' 一样导入它,如果你只用那个。你可以找到一个例子(使用时刻)here
    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 2017-04-25
    • 2017-10-07
    • 2016-09-13
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    相关资源
    最近更新 更多