How to import a module that extends another on the same namespace

孤者浪人 提交于 2019-12-11 02:25:19

问题


A simple road block. I need to use Leaflet and the Leaflet-Draw plugin in some of my services.

I don't know how to import the complete module (core and plugin)

// the core
import * as L from 'leaflet';
// extension
import 'leaflet-draw';

export class LeafletConsumerService {}

I have a solution that I don't like much. I load the libraries by hard-linking them in index.html and the consumer simply has a reference declaration to the typings files

/// <reference path="../typings/index.d.ts" />

export class LeafletConsumerService {}

Is there no other way I can do this? is there a way to import one file that should just cause a side-effect onto an already loaded module?


回答1:


Ok, a quick and dirty solution. This answer works if you start your project from a quickstart repo. Goto system.config.extras.js and extend your configuration as such:

System.config({
    map: {
        'leaflet': 'npm:leaflet/dist/leaflet.js'
    },
    packages: {
        leaflet: {
            defaultExtension: 'js'
        },
    }
});

this means that we can now import leaflet as such

import * as L from 'leaflet';

and add our corresponding extension files as described

import 'your-leaflet-extension';

Remember to extend the type info for your extensions (through a typings file or make your own)



来源:https://stackoverflow.com/questions/41162479/how-to-import-a-module-that-extends-another-on-the-same-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!