2016-04-06 18 views
4

シングルページアプリケーションを作成しようとしていますが、ルーティングが機能しません。私は多くのチュートリアルを使ってみましたが、Angular2がまだベータ版であることを考えると、非常に早く古くなったようです。Angular2ルーティングの使い方

それはそれは、すぐに私は、ルータのディレクティブやルータの設定やルータのプロバイダへの参照を追加すると、アプリが動作を停止し、次のエラーがブラウザのコンソールに出力されているようだ。今私が持っている

Error: Zone</ZoneDelegate</[email protected]://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:332:20 
    Zone</Zone</[email protected]://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:227:25 
    scheduleResolveOrReject/<@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:576:53 
    Zone</ZoneDelegate</[email protected]://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:365:24 
    Zone</Zone</[email protected]://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:263:29 
    [email protected]://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:482:26 
    ZoneTask/[email protected]://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:434:22 

    Evaluating http://localhost:3000/angular2/router 
    Error loading http://localhost:3000/app/main.js 

動作しない、次のファイル:

index.html

<html> 
    <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Rapid</title> 

    <!-- 1. Load libraries --> 
    <!-- IE required polyfills, in this exact order --> 

    <link href="css/dashboard.css" rel="stylesheet"> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 

    <script src="node_modules/es6-shim/es6-shim.min.js"></script> 
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script> 
    <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script> 

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script> 
    <script src="node_modules/systemjs/dist/system.src.js"></script> 
    <script src="node_modules/rxjs/bundles/Rx.js"></script> 
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script> 
    <script src="https://code.angularjs.org/2.0.0-beta.0/http.dev.js"></script> 

    <!-- 2. Configure SystemJS --> 
    <script> 
     System.config({ 
     packages: {   
      app: { 
      format: 'register', 
      defaultExtension: 'js' 
      } 
     } 
     }); 
     System.import('app/main') 
      .then(null, console.error.bind(console)); 
    </script> 
    </head> 

    <!-- 3. Display the application --> 
    <body> 
    <app>Loading...</app> 
    </body> 
</html> 

main.ts

import {MainApp} from './mainApp.component'; 
import {ROUTER_PROVIDERS} from 'angular2/router'; 
import {bootstrap} from 'angular2/platform/browser'; 

bootstrap(MainApp, [ 
    ROUTER_PROVIDERS 
]); 

mainApp.component.ts

import {Component} from 'angular2/core'; 
import {ROUTER_DIRECTIVES, RouteConfig} from 'angular2/router'; 
import {Home} from './home.component' 

@Component({ 
    selector: 'app', 
    template: `  
    <div>Done</div> 

    `, 
    directives: [ROUTER_DIRECTIVES] 
}) 

    @RouteConfig([ 
    { path: '/', component: Home, as: 'Home' } 
    ]) 

export class MainApp {} 

home.component.ts

import {Component} from 'angular2/core';  

@Component({ 
    template: '<div>Home</div>', 
}) 

export class Home {} 

これは初心者の質問であれば申し訳ありませんが、私は昨日からそれに引っかかってきたと私は知りませんどのようにそれを解決するか...

+0

'index.html'に' router.dev.js'がありません。それが問題だろうか? :) – PierreDuc

答えて

4

router.dev.js(付属)& app/main.jsが正しい場所にあります。

あなたはここでいくつかのものを逃しました。

ルート

の変化の下に行うその後、あなたの指定したルートの useAsDefault: trueを使用 <router-outlet></router-outlet>ディレクティブ
  • を持つことになります<base href="/">
  • アプリケーションコンポーネントのテンプレートのようなあなたのページにhref="/"タグをbaseを追加しています
    @RouteConfig([ 
        { path: '/', component: Home, as: 'Home', useAsDefault: true } 
        ]) 
    
  • +1

    こんにちはPankaj!あなたは ''の代わりに ''を意味すると思います... –

    +1

    ああ悪いです..混乱した角度1.5(ng-outlet)の角度2 ..笑。 2ボス.. –

    2

    今あなたが直面しているエラーの問題は、index.htmlrouter.dev.jsが見つからないためです。しかし、@PankajParkarが既に提案しているように、あなたがコードを実際に動作させるためには、もう少し多くのものをコードに追加する必要があります:)

    関連する問題