最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 Selector did not match any elements with bundle and ECM6 - Stack Overflow

programmeradmin4浏览0评论

I have a very simple angular2 project, configured to work with Gulp, Bundle and ECM6. Bundle will create a big file which contains the translated ECM5 of angular plus my app.

<!DOCTYPE html>
<html>
<head>
    <title>Angular 2 Test</title>
    <script src="bundle.js"></script>
</head>

<body>
<mainapp>

</mainapp>
</body>
</html>

The angular app is defined as follows:

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);

However when I load it, I keep on getting an error

"selector 'mainapp' did not match any element"

I have a very simple angular2 project, configured to work with Gulp, Bundle and ECM6. Bundle will create a big file which contains the translated ECM5 of angular plus my app.

<!DOCTYPE html>
<html>
<head>
    <title>Angular 2 Test</title>
    <script src="bundle.js"></script>
</head>

<body>
<mainapp>

</mainapp>
</body>
</html>

The angular app is defined as follows:

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);

However when I load it, I keep on getting an error

"selector 'mainapp' did not match any element"
Share Improve this question asked Jan 12, 2016 at 11:09 birkettbirkett 10.1k2 gold badges22 silver badges26 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 23

The problem was that I was including the bundle.js before the mainapp component was defined in the HTML. This fixed the issue.

<!DOCTYPE html>
<html>
<head>
    <title>Angular 2 Test</title>

</head>

<body>
<mainapp>

</mainapp>
<script src="bundle.js"></script>
</body>
</html>

Update:

<script src="bundle.js" defer></script>

Seems to also solve the problem regardless of the order of the elements.

发布评论

评论列表(0)

  1. 暂无评论