ngx-restangular安装在node_modules文件夹中 - 从Angular服务

时间:2018-02-07 23:18:19

标签: javascript angularjs angular typescript restangular

我正在将AngularJS解决方案升级到Angular 4,我需要将'restangular'库升级为使用ngx-restangular,因为我有一个使用restangular的现有AngularJS服务,我需要将它转换为Angular 4我使用:

安装了ngx-restangular
npm install --save ngx-restangular@1.0.13

这是为Angular 4安装ngx-restangular的正确方法(参见https://www.npmjs.com/package/ngx-restangular#how-do-i-add-this-to-my-project-in-angular-4

我现在已经在node_modules / ngx-restangular下成功安装了ngx-restangular。我正试图在我的新Angular 4服务的顶部导入它,这是使用TypeScript,如下所示:

MyService.ts

import { Injectable } from '@angular/core';
import { RestangularModule, Restangular } from 'ngx-restangular';

@Injectable()
export class MyService {
   ... service code

现在这应该可以正常工作,因为导入工作的方式是当使用绝对路径引用模块时,就像我在这里使用ngx-restangular一样,TypeScript将寻找外部模块。有关其工作原理的详细解释,请参阅Angular2 & TypeScript importing of node_modules

我的应用正在加载@ angular / core fine,它的引用方式与ngx-restangular相同,来自同一个文件。但是使用ngx-restangular,我在控制台中获得了以下堆栈跟踪,但是当我运行应用程序时:

    Failed to load resource: the server responded with a status of 404 (Not Found)
sign-in-redirect:420 Error: (SystemJS) XHR error (404 Not Found) loading http://localhost/ngx-restangular
    Error: XHR error (404 Not Found) loading http://localhost/ngx-restangular
        at XMLHttpRequest.wrapFn (http://localhost/node_modules/zone.js/dist/zone.js:1166:39)
        at ZoneDelegate.invokeTask (http://localhost/node_modules/zone.js/dist/zone.js:421:31)
        at Zone.runTask (http://localhost/node_modules/zone.js/dist/zone.js:188:47)
        at ZoneTask.invokeTask [as invoke] (http://localhost/node_modules/zone.js/dist/zone.js:496:34)
        at invokeTask (http://localhost/node_modules/zone.js/dist/zone.js:1517:14)
        at XMLHttpRequest.globalZoneAwareCallback (http://localhost/node_modules/zone.js/dist/zone.js:1543:17)
    Error loading http://localhost/ngx-restangular as "ngx-restangular" from http://localhost/assets/js/app/MyService.js
        at XMLHttpRequest.wrapFn (http://localhost/node_modules/zone.js/dist/zone.js:1166:39)
        at ZoneDelegate.invokeTask (http://localhost/node_modules/zone.js/dist/zone.js:421:31)
        at Zone.runTask (http://localhost/node_modules/zone.js/dist/zone.js:188:47)
        at ZoneTask.invokeTask [as invoke] (http://localhost/node_modules/zone.js/dist/zone.js:496:34)
        at invokeTask (http://localhost/node_modules/zone.js/dist/zone.js:1517:14)
        at XMLHttpRequest.globalZoneAwareCallback (http://localhost/node_modules/zone.js/dist/zone.js:1543:17)
    Error loading http://localhost/ngx-restangular as "ngx-restangular" from http://localhost/assets/js/app/MyService.js

所以,看看下面这一行似乎SystemJS可能在我的应用程序的根目录中寻找ngx-restangular:

(SystemJS) XHR error (404 Not Found) loading http://localhost/ngx-restangular

为什么会发生这种情况,我如何导入ngx-restangular?我尝试将其添加为脚本标记,但它没有帮助:

<script src="/node_modules/ngx-restangular/dist/umd/ngx-restangular.js"></script>

0 个答案:

没有答案