Angular 2:从创建的路线加载app / main.ts.

时间:2017-01-06 10:35:21

标签: angular router

这是一个Angular 2 RC5项目: 我刚开始向路线添加参数,然后出现此问题: Console print : XHR error loading http://localhost:3000/login/app/main.js

事实上,这是我的路由器:

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

import {HomeComponent} from './components/home/home.component';
import {LoginComponent} from './components/login/login.component';
import {ProfileDetailsComponent} from      './components/profileDetails/profileDetails.component';

const appRoutes: Routes = [
    {
        path: '',
        component: HomeComponent
     },
    {
        path: 'login/:userId',
        component: LoginComponent
    },
    {
        path: 'profile/:userId',
        component: ProfileDetailsComponent
     }
];

export const Routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

所以当我调用/ login / 42时,它会加载LoginComponent:

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

@Component({
  moduleId: module.id,
  selector: 'login',
  templateUrl : "login.component.html"
})

export class LoginComponent { }

这很有效。 问题是当我重新加载页面时,我收到了上一个错误:

(SystemJS) XHR error (404 not Found) loading http://localhost:3000/login/app/main.js

它尝试从我刚刚添加的登录路径加载app / main.ts!

有些主题说这是由index.html中的catch错误引起的:

System.import('app/main').catch(function(err){ console.error(err); });

(编辑)我也把基本标记放在我的index.html文件中:

<base href="/"/>

其他人谈到了systemjs.config.js:

(function (global) {
    System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',
      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',
      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: 'main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

如果您要求我发布其他文件,请不要犹豫! 谢谢!

1 个答案:

答案 0 :(得分:1)

我已经等了很长时间并获取了很多网址。

最后,我没有找到解决方案,而是一种解决问题的方法:

{
    path: 'profile',
    component: ProfileDetailsComponent
 }

使用GET参数传递其他数据:

/profile?id=20

希望有所帮助