简体   繁体   English

如何防止 Leaflet.js 重新加载 Electron 应用程序?

[英]How to prevent Leaflet.js from reloading Electron app?

I have set up an app using Electron.我已经使用 Electron 设置了一个应用程序。 For the most part, all of my code is in the index.html file.大多数情况下,我的所有代码都在 index.html 文件中。 The app takes some data from the user, and uses it to plot areas on a Leaflet map.该应用程序从用户那里获取一些数据,并使用它在传单地图上绘制区域。 The map is plotted onto a <div>地图绘制在<div>

The Leaflet map can be drawn with the taken geolocation, but, when the areas are plotted, the app reloads, causing the data to be lost.可以使用获取的地理位置绘制 Leaflet 地图,但是,在绘制区域时,应用程序会重新加载,从而导致数据丢失。

I have rearranged most of the javascript, to find any issues there.我已经重新排列了大部分 javascript,以查找其中的任何问题。 I have tried global and local variables.我试过全局和局部变量。

    //Geoloaction for now using dev tools to spoof for test location.
    const geo = navigator.geolocation;
    let lat;
    let lon;
    let map;

    document.getElementById("geo").addEventListener("click", () => {
      geo.getCurrentPosition((position) => {
        lat = position.coords.latitude;
        lon = position.coords.longitude;
        document.getElementById('location').value = (lat + ', ' + lon);
        mapIt();
      }, error);
    });

    document.getElementById('submit').addEventListener("click", () => {
      let rad = document.getElementById('radius');
      let squa = document.getElementById('square');
      if (rad.checked) {
        radius();
      } else if (squa.checked) {
        square();
      }
    });

    //Mapping function using Leaflet
    function mapIt() {
      console.log("RUNNING");
      map = L.map('maps').setView([lat, lon], 8);
      const attribution = '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>';
      const tileURL = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
      const tiles = L.tileLayer(tileURL, {
        attribution
      });
      tiles.addTo(map);
    }

    function error(error) {
      console.log(error.POSITION_UNAVAILABLE);
    }

At the end of either of these two functions, the app is caused to reload:在这两个函数中的任何一个结束时,都会导致应用程序重新加载:

    function square() {
      //Some Calculations...

      let bounds = [
        [lat,lon],[lat,lon]
      ];
      L.rectangle(bounds, {
        color: "#ff7800",
        weight: 1
      }).addTo(map);
      map.fitBounds(bounds)
    }

    function radius() {
      //Some Calculations...

      L.circle([lat, lon], {
        radius: (km_R * 1000)
      }).addTo(map);
    }

There should be no need for the app to reload.应用程序应该不需要重新加载。 Everything worked fine, until I added those last two functions.一切正常,直到我添加了最后两个功能。 I have debugged, and the functions run until the end.我已经调试过了,函数一直运行到最后。 No error messages are ejected.不会弹出任何错误消息。

Is this just an issue with an Electron App's format?这只是电子应用程序格式的问题吗? Will it go away, after I build and package the App?在我构建和打包应用程序后,它会消失吗?

Smells like you have an HTML <form> with a <button> or <input type="submit"/> , since you target an element with id "submit".闻起来就像你有一个带有<button><input type="submit"/>的 HTML <form> ,因为你的目标是一个 ID 为“submit”的元素。

If that is the case, the HTML specified behaviour is to send the form data to the page indicated by the action attribute.如果是这种情况,HTML 指定的行为是将表单数据发送到由action属性指示的页面。 If there no such indication, it sends the data to the current page, effectively reloading it.如果没有这样的指示,它将数据发送到当前页面,有效地重新加载它。

See also javascript redirect not working anyway另请参阅javascript 重定向无论如何都不起作用

The easy solution is simply to add event.preventDefault() at the start of your event listener.简单的解决方案是在事件侦听器的开头添加event.preventDefault()

Leaflet itself has no mean to reload your page. Leaflet 本身无法重新加载您的页面。

For refererence for the case of a button: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button按钮案例参考: https : //developer.mozilla.org/en-US/docs/Web/HTML/Element/button

If your buttons are not for submitting form data to a server, be sure to set their type attribute to button .如果您的按钮不是用于向服务器提交表单数据,请确保将它们的type属性设置为button Otherwise they will try to submit form data and to load the (nonexistent) response, possibly destroying the current state of the document.否则他们将尝试提交表单数据并加载(不存在的)响应,可能会破坏文档的当前状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM