【问题标题】:Angular2 RC5 error:zone.js: Unhandled Promise rejection: No provider for ElementRefAngular2 RC5 错误:zone.js:未处理的承诺拒绝:没有提供 ElementRef
【发布时间】:2016-08-12 20:29:07
【问题描述】:

我最近从 RC4 升级到 RC5,并没有更改任何代码,但应用程序已损坏。这是错误消息:

加载 index.html 文件时似乎出现了错误消息:

<html>
<head>
    <script>
        var pjson = require('./package.json');
        document.title = pjson.name;
    </script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="./node_modules/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/global.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/main/bootstrap.js').catch(function(err){ console.error(err); });
    </script>
    <script>
        var electron = require('electron');
    </script>
</head>

<body>
<app-cmp>Loading...</app-cmp>
</body>
</html>

Update:现在使用Angular-CLI,没问题了。

【问题讨论】:

  • 查看您在哪里使用 ElementRef 可能会有所帮助。
  • @LucasTétreault 我有同样的问题,我没有在代码中的任何地方引用 ElementRef。自从尝试通过 NgModule 的重大更改迁移到 RC.5 后,我才看到这一点。
  • @LucasTétreault 我和 MichaelOryl 有同样的问题,我没有在任何地方引用 ElementRef。
  • 尝试在解析中使用 ElementRef 并获得 there is no provider for ElementRef

标签: angular angular2-upgrade


【解决方案1】:

看起来你必须将ElementRef 拉入@NgModule 注释的imports 部分,就像我在my app 的这个示例中所做的那样。

如果这对您来说毫无意义,那么您需要阅读 RC.5 对 Angular 2 所做的重大更改。他们是大佬。这是documentation for NgModule。您会在Quickstart Docs 中找到一个非常基本的使用示例。如果我的个人经验有任何迹象,您的应用程序将需要进行大修才能使其正常工作。几个小时后我还没有让我的再次工作,我的是一个非常基本的应用程序。

// app.module.ts
// Angular 2 imports
import { NgModule, ElementRef } from '@angular/core'; //  <--- Import ElementRef
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router'
import { HTTP_PROVIDERS } from '@angular/http';
import { COMMON_DIRECTIVES } from '@angular/common';

// My components and services (yours will certainly differ)
import {PublicPage} from './components/pages/public-page'
import {ProtectedPage} from './components/pages/protected-page'
import {LoggedoutPage} from "./components/pages/loggedout-page";
import { APP_ROUTES } from './app.routes';
import { WindowService } from './services/window.service';
import { AuthService } from './services/auth.service';
import { CookieService } from './services/cookies.service';

// My main component for this app/module
import { AppComponent }  from './app.component';

@NgModule({
    // Add ElementRef to the module imports below
    imports:      [ ElementRef, BrowserModule, RouterModule.forRoot(APP_ROUTES) ],  
    declarations: [ AppComponent, PublicPage, ProtectedPage, LoggedoutPage ],
    bootstrap:    [ AppComponent ],
    providers:    [ WindowService, AuthService, CookieService, HTTP_PROVIDERS, COMMON_DIRECTIVES ]
})
export class AppModule { }

然后我用这样的东西来引导模块:

// main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);

【讨论】:

  • 但问题是 - 这不是一个突破性的变化。请参阅迁移指南:angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html 如果除了使所有 npm 模块 rc5 之外没有其他任何更改,它“应该”工作!
  • @LucasTétreault 但这将是最终版本中的重大变化。当 Angular 2 的最终版本最终发布时,这种迁移就会消失。因此,无论哪种方式,这都是在 RC 阶段做出的重大改变。您的 RC.4 代码不适用于 Angular 2.0 的最终版本。
  • @MichaelOryl 上面的代码在导入时使用 ElementRef 是否有效?,
  • @MichaelOryl - 我创建了一个简单的项目,在“导入 ElementRef”之后像你一样把它放在 NgModule 中,我收到错误:“TypeError:无法读取 null 的属性'type'”。你以前有过这个错误吗?
  • 我尝试了这个解决方案并得到了(SystemJS) Unexpected value 'ElementRef' imported by the module 'AppModule'(…)。有什么想法吗?
【解决方案2】:

90% 确定 ElementRef 应该是自动导入的。如果这出现了,那么在此过程中已经丢失了一些东西......它不应该手动导入

【讨论】:

  • 我同意这一点!这通常对我来说意味着某些东西被错误地导入了,或者在树的上层某处缺少依赖项。
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多