[英]How to include jQuery Plugin Mapael into Angular 5?
So, I tried following a bit how other questions dealt with the problem of including jQuery Plugins.因此,我尝试了解其他问题如何处理包含 jQuery 插件的问题。 But let's start with the basics first.
但让我们先从基础开始。
First, I installed the jQuery plugin.首先,我安装了 jQuery 插件。
npm i jquery
Then, I added the typescript definition.然后,我添加了打字稿定义。
npm install -d @types/jquery
I included it in my script array.我将它包含在我的脚本数组中。
"apps": [{
...
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
],
...
}]
Then, I tried out if it works.然后,我尝试了它是否有效。
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');
});
}
}
The response came promptly...反应很快……
So far, so good.到目前为止,很好。 First work-package is done, time to continue.
第一个工作包完成,是时候继续了。 I installed then the jQuery plugin mapael
然后我安装了 jQuery 插件 mapael
npm i jquery-mapael
angular-cli.json 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"
],
...
}]
After that I tried this.之后我尝试了这个。 Based on the code example seen on the official page https://www.vincentbroute.fr/mapael/#basic-code-example
基于在官方页面上看到的代码示例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"
}
});
}
}
At first things looked fine, no warnings or problems shown, but when I load the page...起初一切看起来都很好,没有显示警告或问题,但是当我加载页面时......
So, I'm at loss how to do this properly.所以,我不知道如何正确地做到这一点。 I was hoping that I could fix this somehow.
我希望我能以某种方式解决这个问题。 But none of the other few examples have been of much help to me.
但是其他几个例子都没有对我有太大帮助。 I apologize, knowing that there have been similar questions, but I can't really get it to work.
我很抱歉,知道有类似的问题,但我无法真正让它发挥作用。
EDIT 1: The first solution I tried out was from Naga Sai A, but - on my end - it requires some changes, so the compiler won't complain.编辑 1:我尝试的第一个解决方案来自 Naga Sai A,但是 - 就我而言 - 它需要一些更改,因此编译器不会抱怨。 Thanks, Naga Sai A!
谢谢,娜迦赛A!
My .ts looks right this我的 .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"
}
});
}
}
EDIT 2: Placeholder for Peters solution编辑 2:Peters 解决方案的占位符
The imports are correct, but the component still require the imports.导入是正确的,但组件仍然需要导入。 Also, including Raphael.js is NOT required.
此外,不需要包含 Raphael.js。 At least I didn't and the map works splendidly.
至少我没有,而且地图效果很好。 My setup hasn't changed.
我的设置没有改变。 Also, I kept my initial way of declaration.
此外,我保留了我最初的声明方式。
The main issue is with your script imports.主要问题在于您的脚本导入。 You are not importing a required dependency for Raphael.
您没有为 Raphael 导入所需的依赖项。 Also, you need to import any maps you plan on using.
此外,您需要导入您计划使用的任何地图。 Update your angular-cli.json to the following:
将 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" ],
Note from NPM:来自 NPM 的注释:
Note on dependencies: jQuery and Raphael (and Mousewheel, if needed) must be loaded before Mapael in order to work properly.关于依赖项的注意事项:必须在 Mapael 之前加载 jQuery 和 Raphael(以及鼠标滚轮,如果需要)才能正常工作。
Note on maps: map files must be loaded after Mapael in order to work properly.关于地图的注意事项:地图文件必须在 Mapael 之后加载才能正常工作。
https://www.npmjs.com/package/jquery-mapael#directly-in-your-page 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" } }); } }
To achieve expected result, use below option of importing jquery, jquery-mapael and js for world countries map要达到预期的结果,请使用以下选项导入 jquery、jquery-mapael 和 js 以获取世界国家/地区地图
import * as $ from 'jquery';
import 'jquery-mapael';
import 'jquery-mapael/js/maps/world_countries.js';
and in ngOnInit并在 ngOnInit
ngOnInit() {
$(".container").mapael({
map: {
name: "world_countries"
}
});
}
It works as shown in screenshot它的工作原理如屏幕截图所示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.