【问题标题】:angular 2 rc router angular2-polyfills.js:349 Errorangular 2 rc 路由器 angular2-polyfills.js:349 错误
【发布时间】:2016-05-07 01:38:06
【问题描述】:

当我将我的 angular2 测试应用程序从 beta-14 转换为 rc1 时。我收到以下错误

ZoneDelegate.invoke @ angular2-polyfills.js:349 Error: Cannot read property 'toString' of undefined(…)

发出此错误的代码在这里

    346 ZoneDelegate.prototype.invoke = function (targetZone, callback, applyThis, applyArgs, source) {
    347     return this._invokeZS
    348         ? this._invokeZS.onInvoke(this._invokeDlgt, this.zone, targetZone, callback, applyThis, applyArgs, source)
    349         : callback.apply(applyThis, applyArgs);
    350 };

导致错误的代码是 main.ts

import {bootstrap}    from '@angular/platform-browser-dynamic';
import {provide, ComponentRef} from '@angular/core';
import {ROUTER_PROVIDERS } from '@angular/router-deprecated';
// ,LocationStrategy, HashLocationStrategy
import { HTTP_PROVIDERS }    from '@angular/http';
import 'rxjs/Rx';
import {APP_BASE_HREF} from '@angular/common'

import {AppComponent} from './appShell/app.component';
import {appInjector} from './login/auth.injector';
import {Authentication} from './login/auth.service';

bootstrap(AppComponent, [
  ROUTER_PROVIDERS,
  //provide(LocationStrategy, {useClass: HashLocationStrategy}),
  provide(APP_BASE_HREF, {useValue: location.pathname}),
  Authentication,
  HTTP_PROVIDERS
])//.catch(err => console.error(err));
.then((appRef: ComponentRef<any>) => {
  // store a reference to the application injector
  appInjector(appRef.injector);
});

为了向后兼容,我使用了 router-deprecated。此代码用于使用 beta-14。

我使用了来自 https://plnkr.co/edit/yRKhoOelf2uJ3yAsdIMm?p=preview 的 config.js。

LocationStrategy, HashLocationStrategy 被注释掉,因为它不再包含在已弃用的路由器中。是不是换成

provide(APP_BASE_HREF, {useValue: location.pathname})  ?

更重要的是,我该怎么做才能让它发挥作用?

【问题讨论】:

  • 你可以在@angular/common找到LocationStrategyHashLocationStrategy

标签: angular angular2-routing


【解决方案1】:

看起来config.js 是错误的。

我在the doc 中找到了官方plunk,它没有显示任何错误。

http://plnkr.co/edit/wbRp2k67JiXbN2RFlaUc?p=preview

如上所述,您可以在@angular/common 中找到LocationStrategyHashLocationStrategy

【讨论】:

  • 我更改了 config.js,它通过了 main.ts。但是在离开 app.component.ts 时,它给出了这个 browser_adapter.ts:78 异常:错误:未捕获(承诺):异常:错误:0:0 原始异常:没有路由器提供者!。任何的想法?但是您对我最初问题的回答是正确的。原始堆栈跟踪:错误:NoProviderError.BaseException [作为构造函数] (npmcdn.com/@angular/core@2.0.0-rc.1/src/facade/…) 的 NoProviderError.AbstractProviderError [作为构造函数] 的 DI 异常
  • 您在代码中使用Router 吗?如果是这样,它是如何进口的?如果不查看您的 app.component.ts,我无法判断发生了什么
  • 是的。我会做一个单独的后续帖子。在那里见:)
猜你喜欢
  • 1970-01-01
  • 2016-09-16
  • 2016-12-14
  • 2016-12-20
  • 2016-10-04
  • 2016-09-02
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多