gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:20:30 24 4
gpt4 key购买 nike

我正在尝试将我制作的组件导入主组件,但它无法正确呈现 <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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com