【问题标题】:Cannot read property 'HTML' of undefined无法读取未定义的属性“HTML”
【发布时间】:2016-12-21 04:57:09
【问题描述】:

Angular 2.0 RC4 出现这个奇怪的错误

Cannot read property 'HTML' of undefined

index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>My th</title>
  <base href="/">

  {{#unless environment.production}}
  <script src="/ember-cli-live-reload.js" type="text/javascript"></script>
  {{/unless}}
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="styles.css">

  <link rel="stylesheet" href="vendor/bootstrap/dist/css/bootstrap.min.css">

</head>
<body>
   <div class="wrapper">
        <div id="ihc" class="navbar navbar-fixed-top brand">
            <div class="navbar-inner">
                <div class="container-fluid">
                    <div class="nav-collapse collapse"></div>
                </div>
             </div>
        </div>
        <div class="body-content">
             <my-app>Loading...</my-app>
        </div>
    </div>
    {{#each scripts.polyfills}}<script src="{{.}}"></script>{{/each}}
   <script src="vendor/ng2-bootstrap/bundles/ng2-bootstrap.min.js"></script>
    <script>
      System.import('system-config.js').then(function () {
        System.import('main');
      }).catch(console.error.bind(console));
    </script>
   <script src="vendor/ng2-bootstrap/bundles/ng2-bootstrap.min.js"></script>
</body>
</html>

main.ts

bootstrap(AppComponent, [
  disableDeprecatedForms(),
  provideForms(),
  provideRouter(AppRoutes)
  ,HTTP_PROVIDERS,
  provide(LocationStrategy, {useClass: HashLocationStrategy})

])
  .catch(err => console.error(err));

这是来自 DEV 控制台的例外情况

zone.js:461 Unhandled Promise rejection: TypeError: Cannot read property 'HTML' of undefined
        at Object.eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js:29:47)
        at eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js:59:4)
        at eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js:60:3)
        at Object.eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_element_schema_registry.js:18:29)
    Evaluating http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/src/schema/dom_element_schema_registry.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/src/compiler.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/compiler.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/index.js
    Evaluating http://localhost:4200/vendor/@angular/platform-browser-dynamic/index.js
    Evaluating http://localhost:4200/main.js
    Error loading http://localhost:4200/main.js ; Zone: <root> ; Task: Promise.then ; Value: Error: TypeError: Cannot read property 'HTML' of undefined(…)


zone.js:463 Error: Uncaught (in promise): Error: TypeError: Cannot read property 'HTML' of undefined
        at Object.eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js:29:47)
        at eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js:59:4)
        at eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js:60:3)
        at Object.eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_element_schema_registry.js:18:29)
    Evaluating http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/src/schema/dom_element_schema_registry.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/src/compiler.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/compiler.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/index.js
    Evaluating http://localhost:4200/vendor/@angular/platform-browser-dynamic/index.js
    Evaluating http://localhost:4200/main.js
    Error loading http://localhost:4200/main.js
        at Object.eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js:29:47)
        at eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js:59:4)
        at eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js:60:3)
        at Object.eval (http://localhost:4200/vendor/@angular/compiler/src/schema/dom_element_schema_registry.js:18:29)
    Evaluating http://localhost:4200/vendor/@angular/compiler/src/schema/dom_security_schema.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/src/schema/dom_element_schema_registry.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/src/compiler.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/compiler.js
    Evaluating http://localhost:4200/vendor/@angular/compiler/index.js
    Evaluating http://localhost:4200/vendor/@angular/platform-browser-dynamic/index.js
    Evaluating http://localhost:4200/main.js
    Error loading http://localhost:4200/main.js
    at resolvePromise (http://localhost:4200/vendor/zone.js/dist/zone.js:538:32)
    at resolvePromise (http://localhost:4200/vendor/zone.js/dist/zone.js:523:18)
    at http://localhost:4200/vendor/zone.js/dist/zone.js:571:18
    at ZoneDelegate.invokeTask (http://localhost:4200/vendor/zone.js/dist/zone.js:356:38)
    at Zone.runTask (http://localhost:4200/vendor/zone.js/dist/zone.js:256:48)
    at drainMicroTaskQueue (http://localhost:4200/vendor/zone.js/dist/zone.js:474:36)
    at XMLHttpRequest.ZoneTask.invoke (http://localhost:4200/vendor/zone.js/dist/zone.js:426:22)consoleError @ zone.js:463_loop_1 @ zone.js:490drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426

我正在想办法解决这个问题

【问题讨论】:

  • 谁能解释为什么这被否决了?这是一个合法的问题。

标签: javascript angularjs angular typescript undefined


【解决方案1】:

这个错误通常是因为 Angular2 找不到你声明的脚本。它不是返回javascript,而是返回html(通常是索引页面)。

您需要做的是在抛出错误的页面上打开开发工具,然后查看网络选项卡。浏览每个文件并检查响应。其中一个脚本很可能会返回 html 而不是 javascript,因为您的路径不正确或文件丢失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-06
    • 2020-10-30
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多