小编Rya*_*led的帖子

Angular 2中的多个模块

我有一个Angular 2应用程序(RC7),它作为单个组件开始,但很快就会以各种不同(有时甚至是完全不相关)的方式在整个项目中使用.

因此,单个NgModule引导所有组件似乎是一个可怕的想法,有大量的膨胀.我正在努力寻找一种方法来拥有多个模块(模块A将包含组件A,模块B将包含组件B等),并且仍然允许在单个页面上引导多个A2应用程序.

在实践中,我想要实现的是:

Page 1 bootstraps模块A - 这是有效的.

Page 2 bootstraps模块B - 这是有效的.

Page 3 bootstraps模块A和模块B - 这不起作用.

的index.html

<script src="/angular2/node_modules/zone.js/dist/zone.js"></script>
<script src="/angular2/node_modules/reflect-metadata/Reflect.js"></script>
<script src="/angular2/node_modules/systemjs/dist/system.src.js"></script>
<script src="/angular2/systemjs.config.js"></script>


<script type="text/javascript">
    System.import('appOne').catch(function(err){ console.error(err); });
    System.import('appTwo').catch(function(err){ console.error(err); });
</script>
Run Code Online (Sandbox Code Playgroud)

systemjs.config.js

(function (global) {
    System.config({
        paths: {
            'npm:': '/angular2/node_modules/'
        },
        map: {
            appOne: '/angular2/',
            appTwo: '/angular2/',
        },
        packages: {
            appOne: {
                main: './appOne.main.js',
                defaultExtension: 'js'
            },
            appTwo: {
                main: './appTwo.main.js',
                defaultExtension: 'js'
            },
        }
    });
})(this);
Run Code Online (Sandbox Code Playgroud)

AppOne和AppTwo的模块只是引导相关组件(componentOne&componentTwo).但是,两者不会同时在同一页面上呈现.我在控制台中没有错误,但是最后在systemjs.config.js文件中列出的最新软件包是唯一要加载的软件包.

谁能明白为什么它可能不起作用?或者推荐另一种方法来构建我的模块.我宁愿没有列出所有组件,服务等的父模块 …

systemjs angular2-modules angular

6
推荐指数
2
解决办法
6877
查看次数

标签 统计

angular ×1

angular2-modules ×1

systemjs ×1