Angular2 RC5错误:zone.js:未处理的Promise拒绝:没有ElementRef的提供者

时间:2016-08-12 20:29:07

标签: angular angular2-upgrade

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

加载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,不再有任何问题。

2 个答案:

答案 0 :(得分:5)

您似乎必须将ElementRef提取到imports注释的@NgModule部分,例如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);

答案 1 :(得分:1)

90%确定应该自动导入ElementRef。如果出现这种情况,那么一路上就会丢失一些东西......不应该手动导入