Angular:无法添加属性resumeBootstrap,对象不可扩展

时间:2017-07-19 00:03:08

标签: angularjs angular angular-upgrade

我有一个升级/混合AngularJS / Angular 2应用程序,我收到此错误:

  

未处理的承诺拒绝:无法添加属性resumeBootstrap,   对象不可扩展;区域:;任务:Promise.then;值:   TypeError:无法添加属性resumeBootstrap,对象不是   扩展       在UpgradeModule.bootstrap(upgrade_module.ts:259)

该位置的源代码是:

// Make sure resumeBootstrap() only exists if the current bootstrap is deferred
const windowAngular = (window as any)['angular'];
windowAngular.resumeBootstrap = undefined;

我的代码工作正常。拉入最新的角度变化似乎导致了这一点。

这是我的引导代码:

import {platformBrowser} from '@angular/platform-browser';
import {UpgradeModule} from '@angular/upgrade/static';

import {AppModuleNgFactory} from './app_module.ngfactory';

angular.element(document.body).ready(() => {
  platformBrowser()
      .bootstrapModuleFactory(AppModuleNgFactory)
      .then(platformRef => {
        const upgrade =
            platformRef.injector.get(UpgradeModule) as UpgradeModule;
        // This is the line that fails:
        upgrade.bootstrap(document.body, ['foo-obfuscated'], {strictDi: false});
});

更新:我尝试在upgrade.bootstrap中将ngDoBootstrap电话转移到AppModule,就像在Angular升级文档示例中一样,但这并未改变任何内容。

2 个答案:

答案 0 :(得分:0)

解决方法:在引导之前,克隆window.angular对象,使其再次可扩展:

function clone(original: any) {
  const clone: any = {};
  const keys = Object.keys(original);
  for (let i = 0; i < keys.length; i++) {
    clone[keys[i]] = original[keys[i]];
  }
  return clone;
}
(window as any)['angular'] = clone((window as any)['angular']);

platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

答案 1 :(得分:0)

需要在Closure加载之前让我的页面运行此代码:

// Closure Defines specific to AngularJS that must be set before Closure's
// base.js is loaded. See documentation in base.js.

// Sealing classes & modules is incompatible with AngularJS' $inject property.
var CLOSURE_DEFINES = {
  'goog.defineClass.SEAL_CLASS_INSTANCES': false,
  'goog.SEAL_MODULE_EXPORTS': false
};