【问题标题】:How to render multiple angular2 components in one page?如何在一页中呈现多个 angular2 组件?
【发布时间】:2016-02-09 12:33:32
【问题描述】:

我正在处理这个项目,我必须在一页中加载多个组件。 我的目录结构是这样的:

我之前在 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>
有什么方法可以在一页中呈现所有这些组件?

【问题讨论】:

    标签: javascript angularjs rendering angular


    【解决方案1】:

    为此,您需要引导每个组件,如下所述:

    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,“引导多个应用程序”部分。

    【讨论】:

    • 我知道。但我的要求是在一个页面中显示所有使用这些组件标签的组件。这可能吗?
    • 通过多个引导程序,您将在 HTML 入口页面中创建多个应用程序。这样,您就可以在此页面的 body 元素中使用 HTML 元素 component_*...
    • 看看这个帖子:blog.sstorie.com/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2019-01-25
    • 1970-01-01
    • 2023-03-27
    • 2017-07-30
    • 1970-01-01
    相关资源
    最近更新 更多