【发布时间】:2017-09-26 21:32:20
【问题描述】:
我有两个自定义组件:
组件1
@Component({
selector: 'my-custom-component1',
templateUrl: './my-custom-component1.html',
styleUrls: ['./my-custom-component1.css']
})
export class MyCustomComponent1 {
constructor() {
console.log('myCustomComponent1');
}
}
组件2
@Component({
selector: 'my-custom-component2',
templateUrl: './my-custom-component2.html',
styleUrls: ['./my-custom-component2.css']
})
export class MyCustomComponent2 {
constructor() {
console.log('myCustomComponent2');
}
}
我在我的根组件中使用 Component1:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<my-custom-component1></my-custom-component1>`
})
export class AppComponent {
}
我在一个网站上有几个 html 文件。我想在一页中使用这个 Angular 应用程序。
我在file01.html中做:
<!doctype html>
<html>
<head>
<title>file01</title>
...
</head>
<body>
...
<app-root>Loading...</app-root>
<script type="text/javascript" src="inline.bundle.js"></script>
<script type="text/javascript" src="polyfills.bundle.js"></script>
<script type="text/javascript" src="styles.bundle.js"></script>
<script type="text/javascript" src="vendor.bundle.js"></script>
<script type="text/javascript" src="main.bundle.js"></script>
</body>
</html>
一切正常,但是...
在另一个页面中,我想使用 Component2。 我该如何使用它?我一直在尝试这个,但没有成功。
file02.html
<!doctype html>
<html>
<head>
<title>file02</title>
...
</head>
<body>
...
<my-custom-component2></my-custom-component2>
<script type="text/javascript" src="inline.bundle.js"></script>
<script type="text/javascript" src="polyfills.bundle.js"></script>
<script type="text/javascript" src="styles.bundle.js"></script>
<script type="text/javascript" src="vendor.bundle.js"></script>
<script type="text/javascript" src="main.bundle.js"></script>
</body>
</html>
【问题讨论】:
-
您必须在另一页上使用
my-custom-component2进行引导。我相信您有这样的事情:platformBrowserDynamic() .bootstrapModule(AppComponent);您必须在其他页面上对MyCustomComponent2重复相同的操作。
标签: angular