Error: Illegal reassignment to import

后端 未结 1 1814
深忆病人
深忆病人 2021-02-14 20:00

I am trying to import a module into a typescript file and then bundle with Rollup.js.

But I am getting an error message which prevents Rollup from completing.

T

相关标签:
1条回答
  • 2021-02-14 20:32

    Pretty annoying and not really sure why it has to be done this way, but I managed to avoid the error and still get the mapbox-gl module to work by using an assign function to set the accessToken on mapboxgl

    So I changed:

    import * as mapboxgl from 'mapbox-gl';
    
    (mapboxgl as any).accessToken = this.accessToken;
    this.map = new mapbox.Map({...});
    

    To this:

    import * as mapboxgl from 'mapbox-gl';
    
    this.assign(mapboxgl, "accessToken", this.accessToken);
    this.map = new mapboxgl.Map({...});
    
    /*
     *
     * credit to this answer for the assign function:
     * http://stackoverflow.com/a/13719799/2393347
     *
     */
    private assign(obj: any, prop: any, value: any) {
        if (typeof prop === "string")
            prop = prop.split(".");
    
        if (prop.length > 1) {
            var e = prop.shift();
            this.assign(obj[e] =
                    Object.prototype.toString.call(obj[e]) === "[object Object]"
                    ? obj[e]
                    : {},
                prop,
                value);
        } else
            obj[prop[0]] = value;
    }
    
    0 讨论(0)
提交回复
热议问题