【问题标题】:Error 'angular is not defined' while upgrading AngularJS1 component升级 AngularJS1 组件时出现错误“未定义角度”
【发布时间】:2019-10-05 00:24:41
【问题描述】:

我正在尝试将用 AngularJS1 编写的组件升级到 Angular6。在我的示例中,我正在通过扩展放置在文件夹“directive-wrappers”下的“UpgradeComponent”来为所有现有 AngularJS1 组件提供包装器。我已经在本地编写了这个应用程序,它适用于 TestDirectiveWrapperTestDirective2Wrapper 两个组件。

但是当我包含第三个组件TestDirective3Wrapper 时,在加载应用程序时,我得到控制台错误

angular.min.js:127 Error: [$injector:unpr] http://errors.angularjs.org/1.7.5/$injector/unpr?p0=testDirective3DirectiveProvider%20%3C-%20testDirective3Directive
    at angular.min.js:7
    at angular.min.js:46
    at Object.d [as get] (angular.min.js:43)
    at angular.min.js:46
    at Object.d [as get] (angular.min.js:43)
    at Function.push../node_modules/@angular/upgrade/fesm5/static.js.UpgradeHelper.getDirective (static.js:872)
    at new UpgradeHelper (static.js:869)
    at TestDirective3Wrapper.UpgradeComponent (static.js:1170)
    at new TestDirective3Wrapper (TestDirective3Wrapper.ts:9)
    at createClass (core.js:9296) "<app-root _nghost-c0="">"

为了在线模拟相同的应用程序,我在 stackblitz 中创建了相同的 angularjs 指令及其包装器,但在这里我遇到了不同的错误。

谁能帮我解决这个问题并让我加载所有 3 个组件?

【问题讨论】:

  • 你在哪里包含了 angularjs 文件?

标签: angularjs angular ng-upgrade angular-upgrade


【解决方案1】:

我玩过这个,我设法get it to work

缺少一些东西。

  1. 您实际上并未包含 AngularJS 1.x 文件。
  2. 您没有导入原始 AngularJS 应用模块文件或任何指令。

一旦我做了这两件事,它就对我有用。

以下是相关代码:

app.module.ts:

import '../AngularJsApp/app.module'; // added

declare var angular: any; // added

angular
  .module('testApp')
  .directive('appRoot', downgradeComponent({ component: AppComponent }));

AngularJsApp/app.module.js:

'use strict';

const angular = require('angular'); // added

// Define the `testApp` module
angular.module('testApp', []);

// added these 3 lines
require('./test-directive/test-directive.js');
require('./test-directive2/test-directive2.js');
require('./test-directive3/test-directive3.js');

【讨论】:

  • 我在本地进行了您在此处列出的相同更改,但我收到编译异常如下: ./src/AngularJsApp/app.module.js 中的错误模块未找到:错误: Can't resolve 'angular' in 'C:\..\NG6Hybrid\src\AngularJsApp' i 「wdm」:编译失败。我在这里遗漏了什么?
  • @Krishnan 您是否查看了我的答案中链接的 StackBlitz 示例并将其与您的本地代码进行比较?有可能我做了其他我没有在回答中明确指出的更改。
  • 我确实将您的更改与我的代码进行了比较,但没有发现任何差异。即使我添加任何新组件,我也会收到相同的错误“错误:[$injector:unpr] Unknown provider”.. 看起来我在这里遗漏了一些东西......? stackblitz.com/edit/ng6hybrid-xvu8bw
  • 谢谢,问题已解决,现在我可以使用升级加载控制器 - stackblitz.com/edit/ng6hybrid-qzrovd
  • 当我尝试添加一些控制器时,我得到了同样的错误信息。我尝试添加 studentController 和 homePageController,但无法加载它。任何想法为什么我会面临这个问题? stackblitz.com/edit/ng6hybrid-c8h6uv
猜你喜欢
  • 2022-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多