【问题标题】:Angular2 with TypeScript: Importing components into componentsAngular2 与 TypeScript:将组件导入组件
【发布时间】:2016-01-15 18:19:40
【问题描述】:

我正在尝试将我制作的组件导入到主组件中,但它没有正确呈现,只有<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 代码 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>

【问题讨论】:

  • 你的文件夹结构是什么?在导入语句中,您必须指定导入组件的相对路径。如果它们在同一个文件夹中,则应该是:“./eyeComponent”,其中 eyeComponent 是您要从中导入的文件的名称。
  • 查看更新的问题
  • 西蒙的回答会解决这个问题。 :)

标签: typescript angular angular2-directives angular2-template


【解决方案1】:

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

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

【讨论】:

  • 我一直在玩,没有成功
猜你喜欢
  • 2016-09-11
  • 1970-01-01
  • 2022-10-14
  • 2017-01-31
  • 2016-08-13
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
相关资源
最近更新 更多