Angular2 with TypeScript:将组件导入组件
Angular2 with TypeScript: Importing components into components
我正在尝试将我制作的组件导入到主要组件中,但它没有正确呈现,只有 <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' ;
我正在尝试将我制作的组件导入到主要组件中,但它没有正确呈现,只有 <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' ;