我有一个Angular 2应用程序(RC7),它作为一个单独的组件开始,但很快就会以各种不同的(有时是完全不相关的)方式在整个项目中使用。Angular 2中的多个模块
因此,一个单独的NgModule引导所有组件似乎是一个巨大的膨胀的可怕想法。 我很努力地找到一种方法来拥有多个模块(模块A将包含组件A,模块B将包含组件B等),并且仍然允许在单个页面上引导多个A2应用程序。
在实践中,我想实现的是:
1个白手起家模块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>
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);
AppOne和AppTwo的模块简单地引导相关的组分(ComponentOne的& componentTwo)。但是,两者不会同时在同一页面上呈现。我在控制台中没有错误,但无论哪个程序包在systemjs.config.js文件中最后列出,都是唯一要加载的程序包。
任何人都可以看到为什么它可能不工作? 或者,也许建议另一种方式来组织我的模块。 我宁愿没有一个包含列出的所有组件,服务等的父模块 - 这似乎打败了Angular 2的嵌套组件树,并且最终会以指数方式影响页面加载时间。
在此先感谢。
https://www.tutorialspoint.com/angular2/angular2_modules.htm对了解SystemJS和Angular 2模块也非常有帮助。 – RyanGled