`angular2`如何导出组件?

时间:2015-12-22 10:50:15

标签: angular

我正在创建一个这样的组件:(来自样本)

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1> ~ My First Angular 2-0 App By Gulp Automation ~ </h1>'
})

export class AppComponent { } //what it is exporting here?

并导入另一个模块:

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component' //what it's imports here

bootstrap(AppComponent); //it works.
index.html中的

<script>
      System.config({
        packages: {        
          app: {
            format: 'register', //what is means?
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/boot')
            .then(null, console.error.bind(console));
    </script>

我无法理解背后的逻辑,任何人都可以帮助我吗?

提前感谢。

3 个答案:

答案 0 :(得分:6)

我认为这些链接可以为您提供帮助:https://angular.io/guide/quickstarthttps://daveceddia.com/angular-2-dependencies-overview/

实际上,SystemJS负责实现模块逻辑以及它们之间的链接。这就是为什么您需要在HTML页面中包含SystemJS文件(<br/>)并使用systemjs进行配置,最后使用System.config加载Angular2应用程序的主要组件的原因。

您在启动System.import时明确使用SystemJ。在应用程序的其他元素中,隐式使用该工具,尤其是在使用TypeScript时。在这种情况下,一个简单的System.import足以导入另一个模块。如果您查看已转换的文件(JavaScript文件),您将看到使用了SystemJS。

附录 SystemJS配置https://angular.io/docs/ts/latest/quickstart.html#!#systemjs)可以为您提供有关Angular2应用程序的SystemJS配置的一些提示

以下是有关模块格式(import块中的format属性)的文档的链接:https://github.com/systemjs/systemjs/blob/master/docs/module-formats.md。通过使用System.config属性的register值,可以指定System.register用于定义模块。

希望它可以帮到你, 亨利

答案 1 :(得分:2)

  

导出类AppComponent {} //它在这里导出什么?

Architecture Overview指南中简要解释了这一点:

  

export class AppComponent { }

     

export语句告诉TypeScript这是一个AppComponent类是公共的模块,可供应用程序的其他模块访问。

     

当我们需要对AppComponent的引用时,我们将其导入为:

     

import {AppComponent} from './app.component';

     

import语句告诉系统它可以从位于相邻文件中的名为app.component的模块中获取AppComponent。模块名称(AKA模块ID)通常与没有扩展名的文件名相同。

答案 2 :(得分:1)

组件就像一个模块,里面包含一些代码,以及当你导出这个模块时使用的其他模块&#34;模块&#34;

import {AppComponent} from './app.component'

AppComponent是类的名称,angular2知道组件名称&#34; AppComponent&#34;服务,&#39;。/ app.component&#39;表示&#34;相同的目录,找到名为&#34; app.component.ts&#34;

的文件

我是ng2的新手,希望它可以帮到你

相关问题