【问题标题】:How to include jQuery Plugin Mapael into Angular 5?如何将 jQuery 插件 Mapael 包含到 Angular 5 中?
【发布时间】:2018-09-23 22:41:16
【问题描述】:

所以,我尝试了一些其他问题如何处理包含 jQuery 插件的问题。但是,让我们先从基础开始。

首先,我安装了 jQuery 插件。

npm i jquery

然后,我添加了打字稿定义。

npm install -d @types/jquery

我将它包含在我的脚本数组中。

"apps": [{
  ...
  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
  ],
  ...
}]

然后,我尝试了它是否有效。

import { Component, OnInit} from '@angular/core';
import * as $ from 'jquery';

@Component({
  selector: 'app-ww-inventory-map',
  templateUrl: './ww-inventory-map.component.html',
  styleUrls: ['./ww-inventory-map.component.scss']
})
export class WwInventoryMapComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    $(window).click(function () {
      alert('ok');
      });
  }


}

回复很快...

到目前为止,一切都很好。第一个工作包已经完成,是时候继续了。然后我安装了jQuery插件mapael

npm i jquery-mapael

angular-cli.json

"apps": [{
  ...
  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/jquery-mapael/js/jquery.mapael.min.js",
    "../node_modules/jquery-mousewheel/jquery.mousewheel.js"
  ],
  ...
}]

之后我尝试了这个。基于官方页面上看到的代码示例 https://www.vincentbroute.fr/mapael/#basic-code-example

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';

declare global {
  interface JQuery {
    mapael(map: any): JQuery;
  }
}

@Component({
  selector: 'app-ww-inventory-map',
  templateUrl: './ww-inventory-map.component.html',
  styleUrls: ['./ww-inventory-map.component.scss']
})
export class WwInventoryMapComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    $(".container").mapael({
      map: {
        name: "world_countries"
      }
    });
  }
}

一开始看起来还不错,没有显示任何警告或问题,但是当我加载页面时...

所以,我不知道如何正确地做到这一点。我希望我能以某种方式解决这个问题。但是其他几个例子对我没有多大帮助。我很抱歉,知道有类似的问题,但我真的无法让它发挥作用。

编辑 1: 我尝试的第一个解决方案来自 Naga Sai A,但是 - 就我而言 - 它需要一些更改,因此编译器不会抱怨。谢谢,Naga Sai A!

我的 .ts 看起来不错

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import 'jquery-mapael';
import 'jquery-mapael/js/maps/world_countries.js';

declare global {
  interface JQuery {
    mapael(map: any): JQuery;
  }
}

@Component({
  selector: 'app-ww-inventory-map',
  templateUrl: './ww-inventory-map.component.html',
  styleUrls: ['./ww-inventory-map.component.scss']
})
export class WwInventoryMapComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    $(".container").mapael({
      map: {
        name: "world_countries"
      }
    });
  }
}

编辑 2:Peters 解决方案的占位符

导入是正确的,但组件仍然需要导入。此外,不需要包含 Raphael.js。至少我没有,地图工作得很好。我的设置没有改变。另外,我保留了最初的声明方式。

【问题讨论】:

  • 声明 var $:any which may help
  • 我一回家就试试这个!
  • 无论您尝试做什么,都应该避免使用 jquery。你应该以有角度的方式做事。
  • 相信我,我很想避免使用 jQuery,但我还没有看到任何类似的地图设计,这个特殊的任务需要我使用 mapael。 @fastAsTortoise
  • @ak.leimrey,我已经用 mapael 导入更新了我的 anwer,它似乎正在工作,希望它对你有用

标签: javascript jquery angular typescript


【解决方案1】:

主要问题在于您的脚本导入。您没有为 Raphael 导入所需的依赖项。此外,您需要导入您计划使用的任何地图。将您的 angular-cli.json 更新为以下内容:

"scripts": [
  "../node_modules/jquery/dist/jquery.min.js",
  "../node_modules/raphael/raphael.min.js",
  "../node_modules/jquery-mousewheel/jquery.mousewheel.js",
  "../node_modules/jquery-mapael/js/jquery.mapael.min.js",
  "../node_modules/jquery-mapael/js/maps/world_countries.min.js"
],

来自 NPM 的注释:

关于依赖关系的注意事项:必须在 Mapael 之前加载 jQuery 和 Raphael(以及鼠标滚轮,如果需要)才能正常工作。

地图注意事项:地图文件必须在 Mapael 之后加载才能正常工作。

https://www.npmjs.com/package/jquery-mapael#directly-in-your-page

import { Component, OnInit } from '@angular/core';

//jquery declaration
declare var $: any;

@Component({
  selector: 'app-ww-inventory-map',
  templateUrl: './ww-inventory-map.component.html',
  styleUrls: ['./ww-inventory-map.component.scss']
})
export class WwInventoryMapComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    $(".container").mapael({
      map: {
        name: "world_countries"
      }
    });
  }
}

【讨论】:

  • 虽然我喜欢这两种解决方案,但我更喜欢这一种,因为您不厌其烦地为该问题提供更多信息。此外,虽然这个项目没有特别需要,但它更适合可重用的组件。非常感谢彼得!
【解决方案2】:

为达到预期效果,请使用以下选项为世界国家地图导入 jquery、jquery-mapael 和 js

import * as $ from 'jquery';
import 'jquery-mapael';
import 'jquery-mapael/js/maps/world_countries.js';

在 ngOnInit 中

 ngOnInit() {
   $(".container").mapael({
      map: {
        name: "world_countries"
      }
    });
  }

它的工作原理如屏幕截图所示

【讨论】:

  • 为什么你会选择使用 jQuery 库的导入,而不是像我的回答中指出的那样在 angular-cli 中定义它们?我也有点不确定为什么你至少不相信我的答案,它在这个答案前 30 分钟指出了问题?我更愿意将 jQuery 包添加到 angular-cli.json 中,特别是如果这将在多个组件中使用,以免在多个组件中重复导入语句。
  • 是的@Peter,要制作可重用组件,可以将其导入一个组件并重用该组件就可以了,我试图调试问题 - mapel 不是一个函数,正确导入后它可以工作,所以我更新了SO。我将测试您的解决方案并更新我的答案
  • 对不起,我的超级延迟回复。坐火车花了我太长时间。这个解决方案确实工作得很好,但它仍然需要一些更改,所以编译器不会抱怨。我会在我的问题中添加它,也会尝试彼得的回答。
  • 编译器问题可能是由于声明,我在导入 jquery 后将其删除,jquery-mapael
猜你喜欢
  • 2018-02-12
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多