英雄编辑器(Tour of Heroes)
错误详情:
AppComponent_Host.html:1 ERROR Error: The selector "my-app" did not match any elements
at DefaultDomRenderer2.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.DefaultDomRenderer2.selectRootElement (platform-browser.es5.js:2791)
at DebugRenderer2.webpackJsonp.../../../core/@angular/core.es5.js.DebugRenderer2.selectRootElement (core.es5.js:13650)
at createElement (core.es5.js:9178)
at createViewNodes (core.es5.js:12163)
at createRootView (core.es5.js:12092)
at callWithDebugContext (core.es5.js:13475)
at Object.debugCreateRootView [as createRootView] (core.es5.js:12792)
at ComponentFactory_.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactory_.create (core.es5.js:9864)
at ComponentFactoryBoundToModule.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactoryBoundToModule.create (core.es5.js:3333)
at ApplicationRef_.webpackJsonp.../../../core/@angular/core.es5.js.ApplicationRef_.bootstrap (core.es5.js:4768)
View_AppComponent_Host_0 @ AppComponent_Host.html:1
AppComponent_Host.html:1 ERROR CONTEXT DebugContext_
View_AppComponent_Host_0 @ AppComponent_Host.html:1
polyfills.bundle.js:3206 Unhandled Promise rejection: The selector "my-app" did not match any elements ; Zone: <root> ; Task: Promise.then ; Value: Error: The selector "my-app" did not match any elements
at DefaultDomRenderer2.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.DefaultDomRenderer2.selectRootElement (platform-browser.es5.js:2791)
at DebugRenderer2.webpackJsonp.../../../core/@angular/core.es5.js.DebugRenderer2.selectRootElement (core.es5.js:13650)
at createElement (core.es5.js:9178)
at createViewNodes (core.es5.js:12163)
at createRootView (core.es5.js:12092)
at callWithDebugContext (core.es5.js:13475)
at Object.debugCreateRootView [as createRootView] (core.es5.js:12792)
at ComponentFactory_.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactory_.create (core.es5.js:9864)
at ComponentFactoryBoundToModule.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactoryBoundToModule.create (core.es5.js:3333)
at ApplicationRef_.webpackJsonp.../../../core/@angular/core.es5.js.ApplicationRef_.bootstrap (core.es5.js:4768) Error: The selector "my-app" did not match any elements
at DefaultDomRenderer2.webpackJsonp.../../../platform-browser/@angular/platform-browser.es5.js.DefaultDomRenderer2.selectRootElement (http://localhost:4200/vendor.bundle.js:68217:19)
at DebugRenderer2.webpackJsonp.../../../core/@angular/core.es5.js.DebugRenderer2.selectRootElement (http://localhost:4200/vendor.bundle.js:57846:49)
at createElement (http://localhost:4200/vendor.bundle.js:53374:23)
at createViewNodes (http://localhost:4200/vendor.bundle.js:56359:44)
at createRootView (http://localhost:4200/vendor.bundle.js:56288:5)
at callWithDebugContext (http://localhost:4200/vendor.bundle.js:57671:42)
at Object.debugCreateRootView [as createRootView] (http://localhost:4200/vendor.bundle.js:56988:12)
at ComponentFactory_.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactory_.create (http://localhost:4200/vendor.bundle.js:54060:46)
at ComponentFactoryBoundToModule.webpackJsonp.../../../core/@angular/core.es5.js.ComponentFactoryBoundToModule.create (http://localhost:4200/vendor.bundle.js:47529:29)
at ApplicationRef_.webpackJsonp.../../../core/@angular/core.es5.js.ApplicationRef_.bootstrap (http://localhost:4200/vendor.bundle.js:48964:57)
无论是自己写的项目还是官网的源码都会报这个错误。
最后根据测试与对比,发现要把根组件的 @Component 中的 selector 自定义标签改为
selector: 'app-root',
教程源码里是
selector: 'my-app',
1.推测是 angular 通过根组件的 selector: 'app-root'匹配元素,修改后无法匹配。
但不清楚为什么官网的源码没有问题
2.selector 的前缀由.angular-cli.json 的 prefix 属性设置,但即使这里修改成'my'后,依然会报错。只能再改回'app-root'
3.终于知道原因的,因为再 index.html 中引用的'app-root'没有改成'my-app' >_<
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于