使用TypeScript的Angular2:将组件导入组件

时间:2016-01-15 18:19:40

标签: typescript angular angular2-directives angular2-template

我尝试将我制作的组件导入主要组件,但它只是在浏览器上显示<helloworld>Testing...</helloworld>才能正确呈现

这是我的主要component.ts文件:

import {bootstrap} from 'angular2/platform/browser';
import {Component, View} from 'angular2/core';
import {eye} from 'eyeComponent';       //importing components
import {hair} from 'hairComponent' ;

@Component({
selector : 'helloworld',
directives : [hair, eye]
});

@View({
template : `<div>
            <hair></hair>
            <eyes></eyes>
            </div>
`
  })
export class helloworld {

}

bootstrap(helloworld);

这是我导入的组件文件

import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core';

@Component({
selector : 'eyes',
template : `<h1> eyes </h1>`
});

export class eye{

}

bootstrap(eye);

这是另一个component.ts文件:

import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core';

@Component({
selector : 'hair',
template : '<h2>Hair Component <h2>'
});

export class hair{

}

bootstrap(hair);

这是tsconfig,即使用vs code ide

{

"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"
]

}

这是html文件

<html>
<head>
    <title>Angular2 App</title>
     <script src="node_modules/es6-shim/es6-shim.js"></script>
     <script src="node_modules/es6-promise/dist/es6-promise.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>
        System.config({
            packages : {
                app : {
                    format : 'register',
                    defaultExtension : 'js'
                }
            }
        });
        System.import('app/app').then(null, console.error.bind(console));
     </script>


    </head>
    <body>

    <helloworld>Testing...</helloworld>

    </body>
    </html>

1 个答案:

答案 0 :(得分:5)

您需要提供指向文件的链接,例如:

import {eye} from './eyeComponent';       //importing components
import {hair} from './hairComponent' ;