应用没有正确改变路线。如何让routeConfig为我的应用程序工作?

时间:2016-05-29 13:43:32

标签: angularjs typescript angular angular2-routing routeconfig

我的有角度的2个打字稿应用程序组件路径似乎无法正常工作。

当我更改为登录地址栏时,它不会拉入html。 它不会给出控制台错误

这是我的应用组件中的代码:

import { Component } from 'angular2/core';
import { RouteConfig, ROUTER_DIRECTIVES } from 'angular2/router';

import {Login} from './components/login/login';
import {Home} from './components/home/home';

@Component({
  selector: 'my-app',
  template: `
      <ul>
        <li>test</li>
      </ul>
  `
  directives: [ROUTER_DIRECTIVES]
})

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

export class AppComponent {}

索引

<html>
  <head>
    <title>Angular 2 QuickStart</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

    <!-- build:vendor -->    
    <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/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="node_modules/angular2/bundles/router.js"></script>
    <script src="node_modules/angular2/bundles/http.dev.js"></script>
    <!-- endbuild -->

    <!-- build:app -->
    <script src="config.js"></script>
    <!-- endbuild -->
  </head>

  <body>
    <div class="header">
      <div class="navbar navbar-default" role="navigation">
        <div class="container">
          <div class="navbar-header">

            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#js-navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            <a class="navbar-brand" href="#/">Angular2</a>
          </div>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="row">
        <my-app>Loading...</my-app>
      </div>
    </div>
  </body>

</html>

这是我的启动文件

import {bootstrap} from 'angular2/platform/browser';
import {HTTP_PROVIDERS} from 'angular2/http';

import {AppComponent} from './app.component';

import {AuthService} from './services/authService/authService';
import {SocialService} from './services/socialService/socialService';
import {UserService} from './services/userService/userService';
import {OrganisationService} from './services/organisationService/organisationService';
import {NotificationService} from './services/notificationService/notificationService';
import {ApplicationService} from './services/applicationService/applicationService';
import {JobService} from './services/jobService/jobService';
import {MessageService} from './services/messageService/messageService';
import {EmailService} from './services/emailService/emailService';

import {Login} from './components/login/login';
import {Home} from './components/home/home';

import {Environment} from './models/environment/environment';
import {UserProfile} from './models/profile/profile';
import {Organisation} from './models/organisation/organisation';
import {User} from './models/user/user';

bootstrap(AppComponent, [
HTTP_PROVIDERS,
AuthService, 
ApplicationService,
EmailService,
SocialService, 
UserService,
JobService,
MessageService, 
EmailService, 
OrganisationService, 
NotificationService, 
Environment,
Organisation,
Profile,
User,
Home,
Login
]);

这是我的配置文件:

System.config({
  packages: {      
    app: {
      format: 'register',
      defaultExtension: 'js'
    }
  }
});
System.import('app/boot')
      .then(null, console.error.bind(console));

1 个答案:

答案 0 :(得分:0)

组件需要<router-outlet></router-outlet>才能使路由器能够添加路由中定义的组件:

@Component({
  selector: 'my-app',
  template: `
      <ul>
        <li>test</li>
      </ul>
      <router-outlet></router-outlet>
  `
  directives: [ROUTER_DIRECTIVES]
})

ROUTER_PROVIDERS也不见了

bootstrap(AppComponent, [
HTTP_PROVIDERS,
ROUTER_PROVIDERS,
...