2017-03-08 65 views
0

作为Angular 2学习项目的一部分,我正在创建一个测试应用程序。(SystemJS)XHR错误(404 Not Found)loading - cutom服务

我想急切地加载模块称为LoginModule的之一,但是我得到以下错误

错误:(SystemJS)XHR错误(404未找到)装载http://localhost:3000/app/core.js错误:XHR错误(404未找到)装载http://localhost:3000/app/core.js在XMLHttpRequest.wrapFn [按_onreadystatechange](http://localhost:3000/node_modules/zone.js/dist/zone.js:1039:29)[]在Zone.runTask(http://localhost:3000/node_modules/zone.js/dist/zone.js:151:47)[=>]在XMLHttpRequest.ZoneTask.invoke(http://localhost:3000/node_modules/zone.js/dist/zone.js:345:33)[]错误加载http://localhost:3000/app/core.js截至的XMLHttpRequest从http://localhost:3000/app/login/login.service.js “../core”。在XMLHttpRequest.ZoneTask.invoke(http://localhost:3000/node_modules/zone.js/dist/zone.js:345:33)处的Zone.runTask(http://localhost:3000/node_modules/zone.js/dist/zone.js:151:47)[=>]处使用wrapFn [作为_onreadystatechange](http://localhost:3000/node_modules/zone.js/dist/zone.js:1039:29)[] ]错误加载http://localhost:3000/app/core.jshttp://localhost:3000/app/login/login.service.js在addToError(http://localhost:3000/node_modules/systemjs/dist/system.src.js:122:78) “../core”[]在linkSetFailed(http://localhost:3000/node_modules/systemjs/dist/system.src.js:695:21)[]在http://localhost:3000/node_modules/systemjs/dist/system.src.js:495:9 []在Zone.run(http://localhost:3000/node_modules/zone.js/dist/zone.js:113:43)[=>]在http://localhost:3000/node_modules/zone.js/dist/zone.js:535:57 []在Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:151:47)[=>]在drainMicroTaskQueue(http://localhost:3000/node_modules/zone.js/dist/zone.js:433:35)[]在XMLHttpRequest.ZoneTask.invoke(http://localhost:3000/node_modules/zone.js/dist/zone.js:349:25)[]

我试图指其中错误消息类似于上面但是什么也帮我又一些其它的问题。

如果有人能够帮助我解决问题,那将会很棒。

我的目录结构如下

enter image description here

登录-routing.module.ts

import { NgModule } from '@angular/core'; 
import { RouterModule, Routes } from '@angular/router'; 

import { LoginComponent } from './login.component'; 

const routes: Routes = [ 
    { path: 'login', component: LoginComponent } 
]; 

@NgModule({ 
    imports: [ RouterModule.forChild(routes) ], 
    exports: [ RouterModule ], 
}) 
export class LoginRoutingModule{ } 

export const loginRoutableComponents = [ 
    LoginComponent 
]; 

login.module.ts看起来像这样

import { NgModule } from '@angular/core'; 
import { CommonModule } from '@angular/common'; 


import { UserProfileService } from './user-profile.service'; 
import { LoginService } from './login.service'; 
import { LoginRoutingModule, loginRoutableComponents } from './login-routing.module'; 



@NgModule({ 
    imports: [ CommonModule, 
      LoginRoutingModule ], 
    declarations: [ loginRoutableComponents ], 
    providers: [ LoginService, UserProfileService ] 
}) 
export class LoginModule{ } 

登录。 service.ts

import { Injectable } from '@angular/core'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/observable/of'; 
import 'rxjs/add/operator/do'; 
import 'rxjs/add/operator/delay'; 

import { SpinnerService } from '../core'; 
import { UserProfileService } from './user-profile.service'; 

@Injectable() 
export class LoginService { 
    constructor(
     private userProfileService: UserProfileService, 
     private spinnerService: SpinnerService){} 

    login(){ 
     return Observable.of(true) 
       .do(_ => this.spinnerService.show()) 
       .delay(1000) 
       .do(this.toggleLoginState.bind(this)); 
    } 

    logout(){ 
     this.toggleLoginState(false); 
    } 

    private toggleLoginState(val: boolean){ 
     this.userProfileService.isLoggedIn = val; 
     this.spinnerService.hide(); 
    } 
} 

用户profile.service.ts

import { Injectable } from '@angular/core'; 

@Injectable() 
export class UserProfileService{ 
    isLoggedIn: boolean = false; 
} 

login.component.ts

import { Component, OnInit, OnDestroy } from '@angular/core'; 
import { ActivatedRoute, Router } from '@angular/router'; 
import 'rxjs/add/operator/mergeMap'; 
import 'rxjs/add/operator/map'; 
import { Subscription } from 'rxjs/Subscription'; 

import { LoginService } from './login.service'; 
import { UserProfileService } from './user-profile.service'; 
import { ToastService } from '../core'; 

@Component({ 
    moduleId: module.id, 
    // selector: 'login', 
    // templateUrl: 'login.component.html' 
    template: ` 
       <div> 
        <h5>Login component</h5> 
        <div> 
         <button id='login-button' *ngIf='!isLoggedIn' (click)='login()'>Login</button> 
         <button id='logout-button' *ngIf='isLoggedIn' (click)='logout()'>Logout</button> 
        </div> 
       </div> 
       ` 
}) 
export class LoginComponent implements OnInit, OnDestroy { 
    loginSub: Subscription; 

    public get isLoggedIn(){ 
     return this.userProfileService.isLoggedIn; 
    } 

    constructor(
     private loginService: LoginService, 
     private toastService: ToastService, 
     private userProfileService: UserProfileService, 
     private route: ActivatedRoute, 
     private router: Router 
    ){} 
    ngOnInit() { 
    } 
    ngOnDestroy() { 
     this.loginSub.unsubscribe(); 
    } 

    login(){ 
     this.loginSub = this.loginService 
          .login() 
          .mergeMap((loginResult) => this.route.queryParams) 
          .map(qp => qp['redirectTo']) 
          .subscribe(redirectTo => { 
           this.toastService.activate('Logged in sucessessfully.'); 
           let url = redirectTo? redirectTo : ['characters']; 
           this.router.navigate(url); 
          }); 
    } 
    logout(){ 
     this.loginService.logout();   
    } 
} 
+0

看起来像你的系统配置JS的一个问题。问题是因为你正在尝试使用桶(索引)。桶不是自然支持SystemJS。你需要配置你正在使用的桶,或者只需使用'from'../ core/index''来指定桶。见https://github.com/angular/angular.io/issues/1301 –

+0

@peeskillet谢谢。这件事从未在我正在经历的课程中讨论过。谢谢指点我讨论。 –

回答

0

感谢@peeskillet 我发现回答了这个问题。

我使用barrels来减少从一堆feature modules中导入各种组件所需的导入语句的数量。正如Peeskillet在他上面的评论中提到的,桶不是由SystemJS自然支持的。它需要在System.config.js中配置,或者在使用导入语句时必须在login.service中提供桶文件的名称。TS我已经取代

import { SpinnerService } from '../core'; 

import { SpinnerService } from '../core/index'; 

瞧突然在问题中提到的错误消失了:)

相关问题