![](/img/trans.png)
[英]How to Load multiple angular2 components from different folders in one index.html file?
[英]How to render multiple angular2 components in one page?
我正在这个项目中工作,我必须在一个页面中加载多个组件。 我的目录结构是这样的:
我以前在angular2中创建了几个组件。 如果更改main.ts
的组件,则可以一一呈现所有组件。以下是main.ts
1.main.ts的内容
import {bootstrap} from 'angular2/platform/browser'
import {HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/add/operator/map';
import {Component1} from './Component1/Component1.component'
import {Component2} from './Component2/Component2.component'
import {Component3} from './Component3/Component3.component'
import {Component4} from './Component4/Component4.component'
import {Component5} from './Component5/Component5.component'
bootstrap(Component5,[
HTTP_PROVIDERS,
]);
2.index.html
<html> <head> <title>Angular 2 TypeScript App</title> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content="iCETS"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="node_modules\\bootstrap\\dist\\css\\bootstrap.min.css" rel="stylesheet" /> <!-- 1. Load libraries --> <!-- IE required polyfills, in this exact order --> <script src="node_modules/es6-shim/es6-shim.min.js"></script> <script src="node_modules/systemjs/dist/system-polyfills.js"></script> <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="node_modules/rxjs/bundles/Rx.js"></script> <script src="node_modules/angular2/bundles/angular2.dev.js"></script> <script src="node_modules/angular2/bundles/http.dev.js"></script> <!-- 2. Configure SystemJS --> <script> System.config({ packages: { app: { format: 'register', defaultExtension: 'js' } } }); System.import('app/main') .then(null, console.error.bind(console)); </script> </head> <!-- 3. Display the application --> <body> <component_5>Loading...</component_5> //it works //what i want to achieve <component_1>Loading...</component_1> <component_2>Loading...</component_2> <component_3>Loading...</component_3> <component_4>Loading...</component_4> </body> </html>
为此,您需要引导每个组件,如下所述:
import {bootstrap} from 'angular2/platform/browser'
import {HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/add/operator/map';
import {Component1} from './Component1/Component1.component'
import {Component2} from './Component2/Component2.component'
import {Component3} from './Component3/Component3.component'
import {Component4} from './Component4/Component4.component'
import {Component5} from './Component5/Component5.component'
bootstrap(Component1,[ ... ]);
(...)
bootstrap(Component5,[
HTTP_PROVIDERS,
]);
请参见以下链接: https : //angular.io/docs/ts/latest/api/platform/browser/bootstrap-function.html ,“引导多个应用程序”部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.