javascript - 如何从一个 index.html 文件中的不同文件夹加载多个 angular2 组件?

标签 javascript angularjs angular relative-path

我开发了一些 Angular 2 组件。 我的应用程序的目录结构如下。 This is the directory structure of my application

我正在从 index.html 加载每个组件作为

<script>
  System.config({
    map: { 'rxjs': 'node_modules/rxjs' },
    packages: {
        app: { format: 'register', defaultExtension: 'js' },
        'rxjs': {defaultExtension: 'js'}           
    }
  });
  System.import('component_1/app/main')
        .then(null, console.error.bind(console));
</script>

我所做的是,必须只有一个 index.html 并且根据 System.config 中提供的路径我将能够加载不同的组件。 如果我将 index.html 放在每个组件文件夹中,我就可以加载组件,但是我想通过从该索引调用每个组件的 ma​​in.ts 对所有组件只使用一个 index.html .html.

以下是我的代码详情。

  1. 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="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="node_modules\bootstrap\dist\css\bootstrap.min.css" rel="stylesheet" />
    <link href="node_modules\bootstrap\dist\css\style.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({
        map: { 'rxjs': 'node_modules/rxjs' },
        packages: {
            app: { format: 'register', defaultExtension: 'js' },
            'rxjs': {defaultExtension: 'js'}           
        }
      });
      System.import('component_1/app/main')
            .then(null, console.error.bind(console));
    </script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

</html>

2.main.ts

import {bootstrap} from 'angular2/platform/browser'
import {HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/add/operator/map';
import {AppComponent} from './app.component'

bootstrap(AppComponent, [
    HTTP_PROVIDERS
]);

3.app.component.ts

import {Component} from 'angular2/core';
import {NgSwitch, NgSwitchWhen, NgSwitchDefault,NgFor} from 'angular2/common';
import {Http, Response} from 'angular2/http';
import {Observable} from 'rxjs/Rx';
import { DataService } from '../app/services/data.service';

@Component({
    selector: 'my-app',
    providers: [DataService],
    template: '<h1>{{record.data}}</h1>',
    directives: [NgSwitch, NgSwitchWhen, NgSwitchDefault]
})
export class AppComponent {
    public record;    
    constructor(private dataService: DataService) { }
    ngOnInit() {        
        this.dataService.getData()
        .subscribe((customers:any[]) => {
            this.record = customers;
        });   
  }  
}

4.data.service.ts

import { Injectable } from 'angular2/core';
import { Http, Response } from 'angular2/http';
import 'rxjs/add/operator/map';

@Injectable()
export class DataService {
    constructor(private http: Http) { }    
    getData() {
        return this.http.get('../../uicomponent.json')
                        .map((res: Response) => res.json());
    }
}

我已经尝试了所有方法,但我的 index.html 没有重定向到我的 main.ts 来加载我的组件。

最佳答案

您应该将每个 component_* 文件夹定义到您的 SystemJS 配置中:

<script>
  System.config({
    map: {
      'rxjs': 'node_modules/rxjs'
    },
    packages: {
        'component_1': { format: 'register', defaultExtension: 'js' },
        'component_2': { format: 'register', defaultExtension: 'js' },
        (...)
        'rxjs': {defaultExtension: 'js'}           
    }
  });
  System.import('component_1/app/main')
    .then(null, console.error.bind(console));

这样您就可以在导入中使用component_*(例如component_1)。

如果您想保留您的配置并能够使用所有组件,您应该将您的 component_* 文件夹移动到 app 文件夹下...

关于javascript - 如何从一个 index.html 文件中的不同文件夹加载多个 angular2 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35269390/

相关文章:

javascript - 在 Typescript 中模拟 Object.defineProperties

返回 TypeError : Value not an object 的 Javascript removeChild 函数

angularjs - 在对象中创建对象数组 - Node.js

angular - 自定义 Angular FormField 在配置为不触发时发出事件

css - 基于 Angular 中的按钮单击执行不同的动画

javascript - 如何将 1 年添加到开始日期以获取 javascript 中的结束日期

javascript - 用于切换 css 动画暂停的按钮 |运行播放状态

javascript - 为什么我的 ng-repeat 项目没有按预期进行过滤?

angularjs - 在每次状态更改时重置 Angular Factory 数据

angular - Swiper 延迟加载不工作(故障排除)