1

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

未处理的 Promise 拒绝:无法添加属性 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;

我的代码运行良好。引入最新的 Angular 更改似乎是造成这种情况的原因。

这是我的引导代码:

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});
});

更新:我尝试在 Angular 升级文档示例中将upgrade.bootstrap调用移动到ngDoBootstrapAppModule喜欢的位置,但这并没有改变任何东西。

4

2 回答 2

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
};
于 2017-07-20T00:30:40.960 回答
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);
于 2017-07-19T03:01:15.943 回答