javascript - Angular2 选择器与 bundle 和 ECM6 不匹配任何元素

标签 javascript html angular

我有一个非常简单的 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/

相关文章:

javascript - 将鼠标悬停在图像上时更改 <td> 背景颜色

javascript - php 重定向包括增加

javascript - 我该如何修复 : InvalidOperationException upon Session timeout in Ajax WebService call

javascript - 更改子元素的样式以在悬停父页面元素 36 次时显示唯一的 div

typescript - 什么是 Angular 2 中的 `change` 事件

javascript - Mongoose 检索仅插入推送

html - firefox的localstorage目录是哪个物理目录?

HTML CSS 元素的行为不符合我的预期 - 居中

asp.net - 是否可以在一页中多次运行一个 Angular 2 应用程序?

javascript - 保持工具提示元素处于打开状态,直到时间工具提示悬停