2016-08-15 78 views
2

6月份,我開始學習Angular2,當時angular.io quickstart和「英雄之旅」(教程)是基於在路由器棄用和舊的main.ts語法(現在我們有ngModules)。從路由器棄用的2.0.0-rc.2遷移到[email protected]

我在升級這兩件事情時遇到了問題:使用ngModule(app.module.ts)並使用新的路由器。

錯誤:

http://localhost:3000/traceur Failed to load resource: the server responded with a status of 404 (Not Found) localhost/:22 Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/traceur (…)

拋出的index.html在行:

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

main.ts(OLD,作品)

import { bootstrap }  from '@angular/platform-browser-dynamic'; 
import { AppComponent } from './app.component'; 
import { HTTP_PROVIDERS, Http, Headers, Response } from '@angular/http'; 
import 'rxjs/Rx'; // required to use the .map()-Function on a Observable-Object 



// Custom Services 
import { GlobalSessionService } from './global-session.service'; 
import { NavbarMenuService } from './navbar-menu.service'; 

bootstrap(AppComponent, 
    [ 
     HTTP_PROVIDERS, 
     Http, 
     GlobalSessionService, 
     NavbarMenuService 
    ] 
); 

main.ts (新,不起作用)

import { AppComponent } from './app.component'; 
// The browser platform with a compiler 
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 
// The app module 
import { AppModule } from './app.module'; 
// Compile and launch the module 
platformBrowserDynamic().bootstrapModule(AppModule); 

app.module.ts

import { NgModule }  from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 
import { AppComponent } from './app.component'; 
import { HTTP_PROVIDERS, Http } from '@angular/http'; 
import { routing, appRoutingProviders } from './app.routing'; 

// Custom Services 
import { GlobalSessionService } from './global-session.service'; 
import { NavbarMenuService } from './navbar-menu.service'; 


@NgModule({ 
    imports: [ BrowserModule, routing ], 
    declarations: [ AppComponent ], 
    providers: [ 
     appRoutingProviders, 
     HTTP_PROVIDERS, 
     Http, 
     GlobalSessionService, 
     NavbarMenuService 
    ], 
    bootstrap: [ AppComponent ] 
}) 
export class AppModule { } 

舊的路由(在app.component.ts)

@RouteConfig([ 
    { path: '/unsere-angebote', 
    name: 'UnsereAngebote', 
    component: UnsereAngeboteComponent, 
    useAsDefault: true 
    }, 
    { path: '/ihre-loesungen', 
    name: 'IhreLoesungen', 
    component: IhreLoesungenComponent 
    }, 
    ... 

新的路由(在app.routes.ts )

import { Routes, RouterModule } from '@angular/router'; 
... 
import { TrainingsComponent } ... 
... 
import {NewsComponent} from ... 


const appRoutes: Routes = [ 
    ... 
    { path: '/news', 
    component: NewsComponent 
    }, 
    { 
    path: '**', 
    component: PageNotFoundComponent 
    } 
]; 

export const appRoutingProviders: any[] = [ 

]; 

export const routing = RouterModule.forRoot(appRoutes); 

systemjs.config.js

(function(global) { 
    // map tells the System loader where to look for things 
    var map = { 
    'app':      'app', // 'dist', 
    '@angular':     'node_modules/@angular', 
    'rxjs':      'node_modules/rxjs' 
    }; 
    // packages tells the System loader how to load when no filename and/or no extension 
    var packages = { 
    'app':      { main: 'main.js', defaultExtension: 'js' }, 
    'rxjs':      { defaultExtension: 'js' } 
    }; 
    var ngPackageNames = [ 
    'common', 
    'compiler', 
    'core', 
    'http', 
    'platform-browser', 
    'platform-browser-dynamic', 
    'router', 
    'router-deprecated', 
    'upgrade', 
    ]; 
    // Add package entries for angular packages 
    ngPackageNames.forEach(function(pkgName) { 
    packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' }; 
    }); 
    var config = { 
    map: map, 
    packages: packages 
    } 
    System.config(config); 
})(this); 

的package.json

{ 
    "name": "abc-project", 
    "version": "0.1.0", 
    "scripts": { 
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ", 
    "lite": "lite-server", 
    "postinstall": "typings install", 
    "tsc": "tsc", 
    "tsc:w": "tsc -w", 
    "typings": "typings" 
    }, 

    "dependencies": { 
    "@angular/common":     "2.0.0-rc.5", 
    "@angular/compiler":     "2.0.0-rc.5", 
    "@angular/core":      "2.0.0-rc.5", 
    "@angular/http":      "2.0.0-rc.5", 
    "@angular/platform-browser":   "2.0.0-rc.5", 
    "@angular/platform-browser-dynamic": "2.0.0-rc.5", 
    "@angular/router":     "3.0.0-rc.1", 
    "@angular/router-deprecated":   "2.0.0-rc.2", 
    "@angular/upgrade":     "2.0.0-rc.5", 
    "systemjs": "0.19.36", 
    "core-js": "^2.4.1", 
    "reflect-metadata": "^0.1.8", 
    "rxjs": "5.0.0-beta.6", 
    "zone.js": "^0.6.12", 
    "bootstrap": "^3.3.7" 
    }, 

    "devDependencies": { 
    "concurrently": "^2.0.0", 
    "lite-server": "^2.2.2", 
    "typescript": "^1.8.10", 
    "typings":"^1.3.2" 
    } 
} 
+0

只是爲了確認,你已經安裝了所有必需的包? @ angular/router,@ angular/http等。對我來說,加載器正在尋找一些缺失的東西。 –

+0

我有,@ R.Richards。我添加了我的package.json到我的問題 – phip1611

+0

我假設你已經運行'npm install',作爲管理員,並且一切順利。是? –

回答

3

我發現錯誤,我有過類似的問題在這裏:Angular2: error at startup of the app "http://localhost:3000/traceur 404 (Not Found)"

的評論在ts文件的開頭殺死應用程序。好吧,至少在評論中輸入。瘋狂的錯誤..

main.ts -file是這樣的:

/*import { bootstrap }  from '@angular/platform-browser-dynamic'; 
import { AppComponent } from './app.component'; 
import { HTTP_PROVIDERS, Http } from '@angular/http'; 
import 'rxjs/Rx'; // required to use the .map()-Function on a Observable-Object 
*/ 

/* 
// Custom Services 
import { GlobalSessionService } from './global-session.service'; 
import { NavbarMenuService } from './navbar-menu.service'; 

bootstrap(AppComponent, 
    [ 
     HTTP_PROVIDERS, 
     Http, 
     GlobalSessionService, 
     NavbarMenuService 
    ] 
);*/ 


import { bootstrap }  from '@angular/platform-browser-dynamic'; 
import { AppComponent } from './app.component'; 
import { HTTP_PROVIDERS, Http, Headers, Response } from '@angular/http'; 
import 'rxjs/Rx'; // required to use the .map()-Function on a Observable-Object 



// Custom Services 
import { GlobalSessionService } from './global-session.service'; 
import { NavbarMenuService } from './navbar-menu.service'; 

bootstrap(AppComponent, 
    [ 
     HTTP_PROVIDERS, 
     Http, 
     GlobalSessionService, 
     NavbarMenuService 
    ] 
); 
+0

此外,您可以也應該將自己的答案標記爲已接受的答案。 – msanford

相關問題