2015-12-22 31 views
11

我建立这样一个部件:(从样品)`angular2`如何导出组件?

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? 

和导入到另一个模块:

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

I:

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

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

我无法理解背后的逻辑,请帮助我吗?

在此先感谢。

回答

5

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

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

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

附录SystemJS配置https://angular.io/docs/ts/latest/quickstart.html#!#systemjs)可以给你约SystemJS的一个Angular2应用

这里配置一些提示是链接到有关的模块格式的文件(该format属性在System.config块) :https://github.com/systemjs/systemjs/blob/master/docs/module-formats.md。通过使用format属性的register值,可以指定System.register用于定义模块。

希望它可以帮助你, 蒂埃里

1

组件喜欢它里面填充一些代码模块和其它模块当u出口这“模块”

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

AppComponent是类的名称,angular2知道组件名称为“AppComponent”使用该服务,“./app.component”的意思是“同一个目录中,找到文件名为‘app.component.ts’

我是新来的NG2,希望它可以帮助ü

+0

因此,'import'需要整个文件而不是类。是吗? – user2024080

+0

从“库”中导入“模块名称”是有意义的,如果你已经学会了像Python这样的oop语言, “库”可能是角度库如“angular2/core”../ common ..等,否则,你可能导入本地模块,如“app.component”与../,../../,./这个模式 – mckit

2

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

这解释(简述)在Architecture Overview指南:

export class AppComponent { }

出口语句告诉打字稿,这是一个模块,其AppComponent类是公共的,访问的其它模块应用。

当我们需要将AppComponent参考,我们导入它是这样的:

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

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