我有一个非常简单的 angular2 项目,配置为使用 Gulp、Bundle 和 ECM6。 Bundle 将创建一个大文件,其中包含已翻译的 Angular ECM5 和我的应用程序。
<!DOCTYPE html>
<html>
<head>
<title>Angular 2 Test</title>
<script src="bundle.js"></script>
</head>
<body>
<mainapp>
</mainapp>
</body>
</html>
angular 应用定义如下:
import {Component, View, bootstrap} from 'angular2/core';
export class mainComponent {
static get annotations() {
return [
new Component({
selector: 'mainapp'
}),
new View({
template: `<div>Hello!</div>`
})
];
}
}
bootstrap(mainComponent);
然而,当我加载它时,我不断收到错误
"selector 'mainapp' did not match any element"
最佳答案
问题是我在 HTML 中定义 mainapp
组件之前包含了 bundle.js
。这解决了问题。
<!DOCTYPE html>
<html>
<head>
<title>Angular 2 Test</title>
</head>
<body>
<mainapp>
</mainapp>
<script src="bundle.js"></script>
</body>
</html>
更新:
<script src="bundle.js" defer></script>
不管元素的顺序如何,似乎也能解决问题。
关于javascript - Angular2 选择器与 bundle 和 ECM6 不匹配任何元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34742068/