首页
学习
活动
专区
圈层
工具
发布

角4缩放
EN

Stack Overflow用户
提问于 2017-08-28 08:08:33
回答 1查看 7K关注 0票数 3

我是新的角4,并试图找到一个库为平底锅缩放。我在国家预防机制上找不到任何包裹。我试图在我的项目中添加jquery,然后添加了jquery.panzoom,但是由于jquery.panzoom是用JavaScript编写的,而我的angular项目是用TypeScript编写的,所以jquery.panzoom没有@types,所以它不能工作。

我遵循的步骤

1) npm安装--保存jquery

2) npm安装-保存@type/jquery

3)从‘jquery’导入*作为$;

代码语言:javascript
复制
   //till here jquery works fine
    $('.sample').click(function () {
      alert('div clicked.');
    });

3) npm安装--保存jquery.panzoom

代码语言:javascript
复制
   $('#panzoomDiv').panzoom(
      {
        $zoomIn: $('.zoom-in'),
        $zoomOut: $('.zoom-out'),
        $zoomRange: $('.zoom-range'),
        $reset: $('.reset')
      } 
    );

在这里我得到了错误

代码语言:javascript
复制
src/app/app.component.ts (22,22): Property 'panzoom' does not exist on type 
'JQuery<HTMLElement>'.

然后,在通过之后,我添加了

代码语言:javascript
复制
interface JQuery {
  panzoom(options: any): any;
}

到我的typings.d.ts文件。现在它确实编译了,但是给出了这个运行时错误。

代码语言:javascript
复制
 ERROR TypeError: __WEBPACK_IMPORTED_MODULE_1_jquery__(...).panzoom is not a function
at AppComponent.webpackJsonp.117.AppComponent.ngOnInit (app.component.ts:22)
at checkAndUpdateDirectiveInline (core.es5.js:10848)
at checkAndUpdateNodeInline (core.es5.js:12349)
at checkAndUpdateNode (core.es5.js:12288)
at debugCheckAndUpdateNode (core.es5.js:13149)
at debugCheckDirectivesFn (core.es5.js:13090)
at Object.eval [as updateDirectives] (AppComponent_Host.html:1)
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13075)
at checkAndUpdateView (core.es5.js:12255)
at callWithDebugContext (core.es5.js:13475)

有人能帮我重定向到一个正确的解决方案或不同的缩放库吗?

有点像

更新

尝试过这种方法,还是不起作用。

代码语言:javascript
复制
    <div style="display:block; padding: 10px;">
      <button id="btn-zoom-in-workspace"
              class="zoom-out">Zoom Out
      </button>
      <button id="btn-zoom-null-workspace"
              class="reset">Reset
      </button>
      <button id="btn-zoom-out-workspace"
              class="zoom-in">Zoom In
      </button>
      <input type="number" class="zoom-range">
   </div>
   <div id="panzoomDiv" #panzoomDiv>
   <div>

    import * as $ from 'jquery';

    @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
    })
    export class AppComponent implements  AfterViewInit {
    @ViewChild('panzoomDiv') panzoomDiv: ElementRef;
     ngAfterViewInit(): void {
        if (this.panzoomDiv)
           $(this.panzoomDiv.nativeElement).panzoom({
                $zoomIn: $('.zoom-in'),
                $zoomOut: $('.zoom-out'),
                $zoomRange: $('.zoom-range'),
                $reset: $('.reset')
        });
     }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-28 11:34:02

  • npm安装--保存jquery
  • 安装jQueryPan缩放

将这些添加到角-cli.json的脚本数组中。

代码语言:javascript
复制
"scripts": [ 
"../node_modules/jquery/dist/jquery.min.js", 
"../node_modules/jquery.panzoom/dist/jquery.panzoom.min.js" 
],

模板

代码语言:javascript
复制
   <div style="display:block; padding: 10px;">
      <button id="btn-zoom-in-workspace"
              class="zoom-out" #reference>Zoom Out
      </button>
      <button id="btn-zoom-null-workspace"
              class="reset" #reference>Reset
      </button>
      <button id="btn-zoom-out-workspace"
              class="zoom-in" #reference>Zoom In
      </button>
      <input type="number" class="zoom-range">
   </div>
   <div id="panzoomDiv" #panzoomDiv>
   <div>

组件中,使用以下内容

代码语言:javascript
复制
declare var $ : any;

@ViewChild('panzoomDiv') panzoomDiv: ElementRef;
//get all the other element references using elementref and use it in function below
     ngAfterViewInit(): void {
        if (this.panzoomDiv)
           $(this.panzoomDiv.nativeElement).panzoom({
                $zoomIn: $(#reference),
                $zoomOut: $(#reference),
                $zoomRange: $(#reference),
                $reset: $(#reference)
        });
     }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45914424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档