【发布时间】: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