How to import a barrel by folder name only?

后端 未结 3 1183

Angular 2 Barrels

In Angular 2, I\'m trying to get barrels to work as described in the documentation.

The official Angular 2 style guide t

3条回答
  •  暖寄归人
    2020-12-05 05:23

    This is all seems overly complicated.

    There's no need to add anything to map, since app, which should be containing everything is already in there. We can just create an array with the subpackages. In my case that was:

    var subPackageNames = [
            '+heroes',
            '+heroes/shared',
            '+heroes/hero-list',
            '+heroes/hero-detail',
            '+heroes/hero-dashboard'
        ];
    

    and then modify the provided packIndex function to take a second argument

    function packIndex(pkgName, baseName) {
        packages[baseName+pkgName] = { main: 'index.js', defaultExtension: 'js' };
    }
    

    now we can add our sub packages to the packages object just like angular does.

    ngPackageNames.forEach(name => setPackageConfig(name, '@angular/'));
    subPackageNames.forEach(name => packIndex(name, 'app/'));
    

提交回复
热议问题