Search code examples
vue.jspopuparcgis-js-apivue-cli-3

Enable arcgis API JS popups with Vue CLI 3


I am trying to get popups from the ArcGIS API JS to display using the framework Vue-CLI 3. But even with a simple sample code, I cannot make it work: Here is the code in vanilla JS:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <title>Popup actions | Sample | ArcGIS API for JavaScript 4.17</title>

    <style>
        html,
        body,
        #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>

    <link rel="stylesheet" href="https://js.arcgis.com/4.17/esri/themes/light/main.css" />
    <script src="https://js.arcgis.com/4.17/"></script>

    <script>
        require([
            "esri/Map",
            "esri/layers/FeatureLayer",
            "esri/views/MapView",
            "esri/geometry/geometryEngine"
        ], function (Map, FeatureLayer, MapView, geometryEngine) {
            // Create the Map
            var map = new Map({
                basemap: "gray-vector"
            });

            // Create the MapView
            var view = new MapView({
                container: "viewDiv",
                map: map,
                center: [-117.08, 34.1],
                zoom: 11
            });

            var template = {
                // autocasts as new PopupTemplate()
                title: "Trail run",
                content: "{name}",
            };

            var featureLayer = new FeatureLayer({
                url:
                    "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/TrailRuns/FeatureServer/0",
                outFields: ["*"],
                popupTemplate: template,
            });
            map.add(featureLayer);
        });
    </script>
</head>

<body>
    <div id="viewDiv"></div>
</body>

</html>

As you can see, when clicking a feature, a popup appears.

If I try to implement this in my vue-CLI project I can not make it work. When clicking on the a feature, it will highlight, but nothing will open. Here is my code .vue:

<template>
    <div></div>
</template>

<script>
    import { loadModules } from "esri-loader";

    export default {
        name: "web-map",
        mounted() {
            // lazy load the required ArcGIS API for JavaScript modules and CSS
            loadModules([
                "esri/Map",
                "esri/views/MapView",
                "esri/layers/FeatureLayer",
            ]).then(([ArcGISMap, MapView, FeatureLayer]) => {
                const map = new ArcGISMap({
                    basemap: "topo-vector",
                });

                this.view = new MapView({
                    container: this.$el,
                    map: map,
                    center: [-117.08, 34.1],
                    zoom: 11,
                });

                var template = {
                    // autocasts as new PopupTemplate()
                    title: "Trail run",
                    content: "{name}",
                };

                var featureLayer = new FeatureLayer({
                    url:
                        "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/TrailRuns/FeatureServer/0",
                    outFields: ["*"],
                    popupTemplate: template,
                });
                map.add(featureLayer);
            });
        },
        beforeDestroy() {
            if (this.view) {
                // destroy the map view
                this.view.destroy();
            }
        },
    };
</script>

<style scoped>
    div {
        padding: 0;
        margin: 0;
        width: 100%;
        height: 100%;
    }
</style>

Am I missing something? (I followed arcgis api js tutorial to get it work in vue-cli -https://developers.arcgis.com/javascript/latest/guide/vue/ ) Thank you very much for your help!

Julien


Solution

  • Your code is fine, you are just not adding css. You are missing { css: true }, that's all.

    loadModules(
      ["esri/Map", "esri/views/MapView", "esri/layers/FeatureLayer"],
      { css: true } // <-- HERE
    ).then(([ArcGISMap, MapView, FeatureLayer]) => {
      //...
    }
    
    

    HERE code with the fix.