首页 > 解决方案 > 从外部文件获取 Javascript 值

问题描述

var locationsJavascript 文件中位置的详细信息。我想从外部的 JS 文件中整合这些位置的详细信息。

....

function mainMap() {
        function locationData(locationURL, locationPrice, locationPriceDetails, locationImg, locationTitle, locationAddress) {
            return ('<a href="' + locationURL + '" class="listing-img-container"><div class="infoBox-close"><i class="fa fa-times"></i></div><div class="listing-img-content"><span class="listing-price">' + locationPrice + '<i>' + locationPriceDetails + '</i></span></div><img src="' + locationImg + '" alt=""></a><div class="listing-content"><div class="listing-title"><h4><a href="#">' + locationTitle + '</a></h4><p>' + locationAddress + '</p></div></div>')
        }


var locations = [
[locationData('link1', '$275,000', '$520 / sq ft', 'img.jpg', 'Eagle Apartmets', "9364 School St. Lynchburg, NY"), 40.7427837, -73.11445617675781, 1, markerIcon], 
[locationData('link2', '$135,000', '$120 / sq ft', 'img.jpg', 'Stack Apartmets', "1234 School St. Lynchburg, NY"), 40.7317837, -73.11435617675781, 1, markerIcon],
,];


var mapZoomAttr = $('#map').attr('data-map-zoom');
        var mapScrollAttr = $('#map').attr('data-map-scroll');
        if (typeof mapZoomAttr !== typeof undefined && mapZoomAttr !== false) {
            var zoomLevel = parseInt(mapZoomAttr);
        } else {
            var zoomLevel = 5;
        }
....

我做了什么?

PHP 文件:

[...]
echo "<script>var locationDetails = [[locationData('link1','$275,000','$520 / sq ft', 'img.jpg', 'Eagle Apartmets', "9364 School St.Lynchburg, NY"), 40.7427837, -73.11445617675781, 1, markerIcon], [locationData('link2', '$135,000', '$120 / sq ft', 'img.jpg', 'StackApartmets', "1234 School St. Lynchburg, NY"), 40.7317837, -73.11435617675781,1, markerIcon],];
[...]

JS 文件:

var locations = locationDetails;

结果:

不工作。

为什么它不起作用?我究竟做错了什么?谢谢你。

完整版的js文件

标签: javascript

解决方案


您可以在 MDN上阅读有关导入导出的信息。

导出示例:

export const one = 1

// or to export multiple
const two = 2
const three = 3

export {
  two,
  three
}

导入示例:

import { one, two, three } from './export' // path of export
console.log(one, two, three)

推荐阅读