Angular2 选择器不匹配嵌套组件中的任何元素
Angular2 Selector did not match any elements in nested Components
我有两个嵌套的 @Components
en angular 2. 视图呈现得很好,但第一次总是抛出 javascript 错误。
这是我在 Typescript 中的代码。
应用程序 HTML:
<body>
<my-app>loading...</my-app>
</body>
应用组件:
import {bootstrap, Component} from 'angular2/angular2';
import {CanvasComponent} from "./CanvasComponent";
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<h2>My Games</h2>
<div>
<my-canvas></my-canvas>
</div>
`,
directives: [CanvasComponent]
})
class AppComponent {
}
bootstrap(AppComponent);
Canvas 分量:
import {bootstrap, Component, View} from 'angular2/angular2';
@Component({
selector: 'my-canvas'
})
@View({
template: `
<div>
<span>Balls:</span>
<div>{{canvas.length}}</div>
</div>
`
})
export class CanvasComponent {
canvas = [1,2,3];
}
bootstrap(CanvasComponent);
错误是:
EXCEPTION: The selector "my-canvas" did not match any elements
从您的 CanvasComponent
文件中删除 bootstrap(CanvasComponent)
。它正在尝试 bootstrap 应用程序第二次使用 CanvasComponent
作为根并在您的应用程序 HTML 中寻找 my-canvas
元素。当然是找不到了。
我通过更改 index.html 中的名称解决了这个问题,您应该确保 index.html 中的标签与主要组件中的选择器相同。
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="stylesheets/styles.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app> <!-- THIS TAG SHOULD BE THE SAME THAT THE SELECTOR IN THE MAIN COMPONENT -->
</body>
</html>
<!-- end snippet -->
</body>
</html>
在我的 index.html 中,我丢失了
<base href="/">
所以需要的最小值是
<!doctype html>
<head>
<base href="/">
</head>
<body>
<your-selector></your-selector>
</body>
</html>
我有两个嵌套的 @Components
en angular 2. 视图呈现得很好,但第一次总是抛出 javascript 错误。
这是我在 Typescript 中的代码。
应用程序 HTML:
<body>
<my-app>loading...</my-app>
</body>
应用组件:
import {bootstrap, Component} from 'angular2/angular2';
import {CanvasComponent} from "./CanvasComponent";
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<h2>My Games</h2>
<div>
<my-canvas></my-canvas>
</div>
`,
directives: [CanvasComponent]
})
class AppComponent {
}
bootstrap(AppComponent);
Canvas 分量:
import {bootstrap, Component, View} from 'angular2/angular2';
@Component({
selector: 'my-canvas'
})
@View({
template: `
<div>
<span>Balls:</span>
<div>{{canvas.length}}</div>
</div>
`
})
export class CanvasComponent {
canvas = [1,2,3];
}
bootstrap(CanvasComponent);
错误是:
EXCEPTION: The selector "my-canvas" did not match any elements
从您的 CanvasComponent
文件中删除 bootstrap(CanvasComponent)
。它正在尝试 bootstrap 应用程序第二次使用 CanvasComponent
作为根并在您的应用程序 HTML 中寻找 my-canvas
元素。当然是找不到了。
我通过更改 index.html 中的名称解决了这个问题,您应该确保 index.html 中的标签与主要组件中的选择器相同。
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="stylesheets/styles.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app> <!-- THIS TAG SHOULD BE THE SAME THAT THE SELECTOR IN THE MAIN COMPONENT -->
</body>
</html>
<!-- end snippet -->
</body>
</html>
在我的 index.html 中,我丢失了
<base href="/">
所以需要的最小值是
<!doctype html>
<head>
<base href="/">
</head>
<body>
<your-selector></your-selector>
</body>
</html>