当前位置: 首页 > 知识库问答 >
问题:

Angular2-选择器my-app不匹配任何元素(仅在第一次加载时)

诸葛雨泽
2023-03-14

有人知道我第一次在Angular2应用程序中加载页面时出现此错误的原因吗?当我浏览我的应用程序时,它会清除,不会回来。如果我按CTRL-F5键,它会返回(虽然不是每次都会)。此应用程序的路由如下所示:

AppComponent.ts
  LoginComponent.ts
  DashboardComponent(Parent route with its own child routes)
    MainDashboardComponent
    EmployeesComponent

顶级索引。html文件具有

错误详细信息:

例外:选择器my-app不匹配任何元素BrowserDomAdapter.log错误@angular2.dev.js:22823BrowserDomAdapter.log组@angular2.dev.js:22834ExceptionHandler.call@angular2.dev.js:1163(匿名函数) @ <--_notifyOnError@angular2.dev.js:13405collection_1。StringMapWrapper.merge.on错误@angular2.dev.js:13309run@angular2-polyfills.js:141(匿名函数)@angular2.dev.js:13328zoneBoundFn@angular2-polyfills.js:111lib$es6$promise$$内部$$try Catch@angular2-polyfills.js:1511lib$es6$promise$$内部$$调用回调@angular2-polyfills.js:1523lib$es6$promise$$内部$$发布@angular2-polyfills.js:1494(匿名函数)@angular2-polyfills.js:243微任务@angular2.dev.js:13360运行@angular2-polyfills.js:138(匿名函数)@angular2.dev.js:13328zoneBoundFn@angular2js: 1305 angular2. dev. js: 22823 STACKTRACE: BrowserDomAdapter. logError@angular2. dev. js: 22823ExceptionHandler. call@angular2. dev. js: 1165(匿名函数)@angular2. dev. js: 12481NgZone._notifyOnError@angular2. dev. js:13405collection_1。StringMapWrapper. merge. onError@angular2. dev. js: 13309run@angular2-Polyfills. js: 141(匿名函数)@angular2. dev. js: 13328zoneBoundFn@angular2-Polyfills. js: 111lib$es6$promise$$内部$$try Catch@angular2-Polyfills. js: 1511lib$es6$promise$内部$$调用@angular2-Polyfills. js: 1523lib$es6$promise$内部$$发布@angular2-Polyfills. js: 1494(匿名函数)@angular2-polfills. js: 243microment@angular2. dev. js: 13360run@angular2-polfills. js: 138(匿名函数)@angular2. dev. js: 13328zoneBoundFn@angular2-polfills. js: 111lib$es6$promise$asap$$flush@angular2-polfills. js: 1305 angular2. dev. js: 22823错误:选择器“我的应用程序”与新BaseExcture中的任何元素不匹配(超文本传输协议://localhost: 3000/node_modules/angular2/bundles/angular2. dev. js: 7104:21)在DomRenderer. selectRootElement(超文本传输协议://localhost: 3000/node_modules/angular2/bundles/angular2. dev. js: 13724:15)在HostViewFactory。viewFactory_HostAppComponent0[作为视工厂](viewFactory_HostAppComponent: 72:18)在AppViewManager_. createRootHostView(超文本传输协议://localhost: 3000/node_modules/angular2/bundles/angular2. dev. js: 9226: 34)在Zone. run(超文本传输协议://localhost: 3000/node_modules/angular2/bundles/angular2. dev. js: 12246:46)在Zone. run(超文本传输协议://localhost: 3000/node_modules/angular2/bundles/angular2-Polyfills. js: 138:17)在ZoneBoundFn(超文本传输协议://localhost: 3000/node_modules/angular2/bundles/angular2. dev. js: 13328:32)传输协议://localhost: 3000/node_modules/angular2/bundles/angular2-polfills. js: 111:19)在lib$es6$promise$$内部$$tryCatch(超文本传输协议://localhost: 3000/node_modules/angular2/bundles/angular2-polfills. js: 1511:16)在lib$es6$promise$$内部$$调用(超文本传输协议://localhost: 3000/node_modules/angular2/bundles/angular2-polfills. js: 1523:17)

-----异步间隙------在_GetStackTraceWithUnCughtError处出错(http://localhost:3000/node_modules/angular2/bundles/angular2-多填料。js:2195:26)在分区。叉子(http://localhost:3000/node_modules/angular2/bundles/angular2-多填料。js:2253:40)在分区。束缚(http://localhost:3000/node_modules/angular2/bundles/angular2-多填料。js:109:48)在bindArguments(http://localhost:3000/node_modules/angular2/bundles/angular2-多填料。js:980:29)在lib$es6$promise$promise$$promise。obj。(匿名功能)[与当时一样](http://localhost:3000/node_modules/angular2/bundles/angular2-多填料。js:1000:37)在DynamicComponentLoader上。加载根(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:12245:49)在迪尤1号。提供使用工厂(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:12343:39)在喷油器处_实例化(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:11354:19)在喷油器处_实例化提供者(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:11287:21)在喷油器处_新的(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:11277:19)

-----异步间隙------在_GetStackTraceWithUnCughtError处出错(http://localhost:3000/node_modules/angular2/bundles/angular2-多填料。js:2195:26)在分区。叉子(http://localhost:3000/node_modules/angular2/bundles/angular2-多填料。js:2253:40)在NgZone_createInnerZone(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:13316:39)在新工业区(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:13182:32)在createNgZone(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:12365:12)在平台上。应用(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:12440:31)反对。引导(http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:24543:64)执行时(http://localhost:3000/app/main.js:15:23)至少,重新评估(http://localhost:3000/node_modules/systemjs/dist/system.src.js:3202:26)反对。执行(http://localhost:3000/node_modules/systemjs/dist/system.src.js:3323:13)浏览器适配器。logError@angular2。dev.js:22823异常处理程序。打电话给@angular2。dev.js:1166(匿名函数)@angular2。dev.js:12481NgZone_notifyOnError@angular2。dev.js:13405collection_1。StringMapWrapper。合并onError@angular2。dev.js:13309run@angular2 polyfills。js:141(匿名函数)@angular2。dev.js:13328zoneBoundFn@angular2 polyfills。js:111lib$es6$promise$$internal$$tryCatch@angular2 polyfills。js:1511lib$es6$promise$$internal$$invokeCallback@angular2 polyfills。js:1523lib$es6$promise$$internal$$publish@angular2 polyfills。js:1494(匿名函数)@angular2 polyfills。js:243微任务@angular2。dev.js:13360run@angular2 polyfills。js:138(匿名函数)@angular2。dev.js:13328zoneBoundFn@angular2 polyfills。js:111lib$es6$promise$asap$$flush@angular2 polyfills。js:1305


共有3个答案

郑锦
2023-03-14

在你的索引中。html应该与你的应用程序匹配。路由器的ts组件选择器

@Component({
  selector: 'my-app',
  directives: [ROUTER_DIRECTIVES],
  template: `

///////////////////

<body>
    <script>
        System.config({
            packages: {
                appScripts: {
                    defaultExtension: 'js'
                }
            }
        });
        System.import('appScripts/app')
              .then(null, console.error.bind(console));
    </script>
       <my-app></my-app> <!--  -->
</body>
王骏
2023-03-14

我猜索引中没有元素。与AppComponent中的选择器匹配的html

林彬
2023-03-14

可能您应该添加

您的脚本应该在DOM就绪后运行。

更新:

<body>
  <my-app>Loading...</my-app>
  <script>
    //Your scripts
  </script>
</body>

 类似资料:
  • 问题内容: 如何从这样的列表中选择仅父级的链接元素? 所以在css中,但不是 谢谢 问题答案: 但是,如果您要专门针对最外层的ul,则需要在根ul上设置一个类: 然后是: 确保您只有root li元素的另一种方法: 它看起来很笨拙,但可以解决问题

  • 问题内容: 我有3个具有相同ID的按钮,当他被点击时,我需要获取每个按钮的值。 这是我当前的脚本: 但这仅适用于第一个按钮,单击其他按钮将被忽略。 问题答案: 我有3个具有相同ID的按钮… 您的HTML无效,同一页面中的元素不能超过一个。 引用规格: 7.5.2 元素标识符:id和class属性 id =名称[CS] 此属性为元素分配名称。此名称在文档中必须唯一。 解决方案 :从更改为, 和 jQ

  • 我有“ngx引导”:“^3.1.4”和 这些完全兼容吗? 我试图打开组件在模态这个问题:ngx-bootstrap如何打开一个模态从另一个组件? 但是我得到了这个错误: 错误错误:未捕获(promise中):错误:StaticInjectorError(AppModule)[DialogBoxComponent- 我已经导入ngx在我的模块为:

  • 问题内容: 我对CSS选择器有疑问。仅当在具有类名的a中时,如何选择具有特定类名的a ?这个CSS类在其他地方使用,我不想在任何地方更改样式。 问题答案: 只需在父元素和后代元素之间使用CSS后代选择器(空格)即可: 在这种情况下,仅适用于Class的规则,前提是其祖先是该Class的规则。相反,您可以使用直接子组合器,但是您必须指定与每个父/祖先的关系,直到需要其类的父子/祖先,这可能很难维护C

  • 问题内容: 我正在尝试使用SeleniumRC测试我的GWT应用程序,并试图使用CSS选择器来匹配元素。 我想计算以下HTML中已启用按钮的数量。 如果是下一个按钮被启用用和禁用,如果它是下用。 因此,基本上,我想用class 检索all下的按钮数量。 问题答案: 据我所知,您不能使用CSS选择器来执行此操作,但是Selenium中有一个命令可以通过XPath进行计数。以下命令将验证有两个禁用的按