typescript - 带有 TypeScript : Importing components into components 的 Angular2

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

最佳答案

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

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

关于typescript - 带有 TypeScript : Importing components into components 的 Angular2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34817332/

相关文章:

javascript - 将 JSON 响应映射到 TypeScript 中的组件模型。

javascript - 使用 angular2 显示 Bootstrap 警报

reactjs - 类型 'loginToken' 上不存在属性 '{ loginToken: string; } | { error: Error; } | { username: string; password: string; }'

json - 如何从 package.json 加载 Angular (v4+)应用程序中的版本号?

html - 在 Angular 2 的表格中显示弹出窗口

Angular 4 动画不适用于 Safari iOS 9.3

javascript - Ionic2:当你来到页面时,你如何选择要关注的输入?

angular - 从指令实例化组件

gulp - Angular2 ng如果不在部署环境中工作

javascript - React 中的子组件应该如何以一种干净且可维护的方式相互通信?