2015-12-22 48 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)通常與沒有擴展名的文件名相同。