简体   繁体   中英

Error in importing class in JavaScript object not defined

I am struggling to try to import a class in Javascript (located in the leaflet-m2t.js file) is there an error keeps getting thrown m2t is not defined. The file is getting imported just fine, and the error is not in the file but in my code. I would appreciate some help in addressing this problem

require('./leaflet-m2t.js')
$scope.tileProvider = new m2t.tileProvider({
    map: map,
    tileUrl: '/m2t',
    defaultLayer: 'Dark',
    tileLayers: [],
    outlineLayers: ['Light', {
        name: 'Dark',
        style: {
            "color": "#737373",
            "weight": 1,
            "fillOpacity": 1,
            "fillColor": "#0d0d0d",
            'backgroundColor': '#242425',
            'clickable': false
        }

                }]
});
var m2t = require('./leaflet-m2t.js');

Unlike in Java you should define what you are importing, in this case the default export of leaflet. JavaScript doesn't magically know what you're referring to, this only works for global variables.

If leaflet uses es6 exports you will need to do:

var m2t = require('./leaflet-m2t.js').default;

Or if you have your app configured to handle es6 imports:

import m2t from './leaflet-m2t.js';

好的,所以解决这个问题的方法恰好是代替使用require语句通过Javascript导入类,而是不得不通过HTML标记<script src="./leaflet-m2t.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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