简体   繁体   中英

Angular2 with TypeScript: Importing components into components

I'm trying to import component that I made, into main component, but it's not rendering correctly only <helloworld>Testing...</helloworld> is displayed on browser

this is my main component.ts file:

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);

Here is my component file that i was importing

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

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

export class eye{

}

bootstrap(eye);

Here is another component.ts file:

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

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

export class hair{

}

bootstrap(hair);

and this is tsconfig, im using vs code ide

{

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

}

And here is html file

<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>

You'll need to provide links to the files,such as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM