如何使用JQVmap与Angular-CLI?

时间:2017-02-07 15:31:57

标签: angular angular-cli jqvmap

我制作了一个运作良好的Angular-CLI项目。但我需要使用jQvmap(here)绘制交互式地图。在我的angular-cli.json文件中,我包含了jqvmap.js和jqvmap.css的路径:

{
  "project": {
    "version": "1.0.0-beta.25.5",
    "name": "db-client"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "styles.css",
        "style/bootstrap-3.3.6/css/bootstrap.css",
        "js/jqvmap/jqvmap.css",
        "style/style.css"
      ],
      "scripts": [
        "js/jquery/jquery-3.1.1.min.js",
        "js/jqvmap/jqvmap.js",
        "js/reductio/reductio.js",
        "js/d3/d3.js",
        "js/crossfilter/crossfilter.js",
        "js/dc/dc.js",
        "js/d3-tip/index.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "inline": {
      "style": false,
      "template": false
    },
    "spec": {
      "class": false,
      "component": true,
      "directive": true,
      "module": false,
      "pipe": true,
      "service": true
    }
  }
}

这是我使用jQvmap的组件:

import { Component } from '@angular/core';
import { MapChartService } from "./mapChart.service";
import { NgModule } from '@angular/core';

@Component({
  selector: 'map-chart',
  templateUrl: './mapChart.component.html',
  styleUrls: ['./mapChart.component.scss']
})
export class MapChartComponent {
  title = 'Module 3 : mapChart';

  constructor(private mapchartservice: MapChartService) {
    this.mapchartservice.startUp()
      .then(function (data) {
        console.log("--->", data);
      });
  }

  ngOnInit() {
    //On récupère la taille visible de l'écran en fonction du navigateur
    //On set la width et height
    //On rend l'écran non scrollable
    if (navigator.appName == 'Netscape' || navigator.appName == 'Firefox') {
      var totalWidth = window.innerWidth
      var totalHeight = window.innerHeight - 140
    }
    else {
      var totalWidth = document.body.clientWidth
      var totalHeight = document.body.clientHeight - 140
    }

    //widht and height
    var widthFirst = totalWidth * (5 / 12)
    var width = totalWidth / 2
    var height = totalHeight / 2
    var width2 = totalWidth / 3
    var height2 = (totalHeight - height) / 2

    //Set size de la map avant import du svg
    document.getElementById('map').setAttribute("style", "width: " + height + "px; height: " + height + "px");

    //Convertisseur x,y des points de la map
    var x = function (coord) {
      return coord * height / 400
    }

    //Création de la map en svg
    $('#map').vectorMap({
      map: 'fr_regions_mill',
      backgroundColor: 'transparent;',
      regionStyle: {
        initial: {
          fill: '#E2E2E2'
        },
        hover: {
          cursor: 'default',
          "fill-opacity": 1
        }
      },
      zoomButtons: false,
      zoomOnScroll: false
    });
  }
}

当我使用npm start命令启动项目时,我遇到了这个问题:

ERROR in /***/client/src/app/mapChart/mapChart.component.ts (49,15): Property 'vectorMap' does not exist on
type 'JQuery'.)

那么,请问如何将jQvmap与angular-cli一起使用?

1 个答案:

答案 0 :(得分:0)

这得益于Sabbir的评论:

  

可能是打字问题。尝试声明let $:any;在导入后的组件文件的顶部。   Sabbir Rahman