Angular 2 routerLink不起作用

时间:2016-08-11 14:14:52

标签: html html5 angular typescript routing

我无法点击<a routerLink="/login">Login</a>从主页导航到登录页面。我已经阅读了Angular 2网站上的教程和开发者指南。

这就是我在代码中的内容:

的index.html:

<html>
    <head>
        <base href="/">
        <title>Portfolio</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="bower_components/jquery/dist/jquery.min.js"></script>
        <!-- Bootstrap -->
        <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
        <link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" href="css/style.css">
        <!-- 1. Load libraries -->
        <!-- Polyfill(s) for older browsers -->
        <script src="node_modules/core-js/client/shim.min.js"></script>
        <script src="node_modules/zone.js/dist/zone.js"></script>
        <script src="node_modules/reflect-metadata/Reflect.js"></script>
        <script src="node_modules/systemjs/dist/system.src.js"></script>
        <!-- 2. Configure SystemJS -->
        <script src="systemjs.config.js"></script>
        <script>
            System.import('app').catch(function (err) {
                console.error(err);
            });
        </script>
    </head>
    <!-- 3. Display the application -->
    <body>
        <my-app>Loading...</my-app>
    </body>
</html>

home.html的:

<nav class="navbar">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <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="#">Portfolio</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home <span class="sr-only">(current)</span></a></li>
                <li><a href="#">Over</a></li>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">C.V.</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a routerLink="/login">login</a></li>
                <li><a href="#">Register</a></li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<router-outlet></router-outlet>
<h1>test</h1>

app.module.ts:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import { routing, appRoutingProviders }     from './app.routing';

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

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

app.component.ts:

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

@Component({
    selector: 'my-app',
    templateUrl : '../home.html',
    directives: [ROUTER_DIRECTIVES]
})
//AppComponent is the root of the application
export class AppComponent { 
    constructor() {
        console.log("We are up and running!");
    }
}

app.routing.ts:

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

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

const appRoutes: Routes = [
    {
        path: 'login',
        component: LoginComponent,
        //    redirectTo: '/login',

        //    pathMatch: 'full'
    }
];
export const appRoutingProviders: any[] = [
    appRoutes
];

export const routing = RouterModule.forRoot(appRoutes);

login.component.ts:

import { Component, OnInit } from '@angular/core';
import { Router }            from '@angular/router';

@Component({
     templateUrl: 'views/login.html'
})
export class LoginComponent  {
     constructor() {
        console.log("we have arrived logincomponent");
    }
}

我做错了什么或者我错过了什么?无论如何,谢谢你的回复。

1 个答案:

答案 0 :(得分:1)

我找不到Angular 2网站上的任何信息来解决我的问题。所以我看了一个教程,所以归功于教程的这个人的链接:https://www.youtube.com/watch?v=Ppy3ipDzngA

有些代码已被弃用,但它确实解决了我的问题。

以下是我添加的代码:

navbar.component.ts:

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

@Component({
    moduleId: module.id,
    selector: 'navbar',
    templateUrl : 'navbar.component.html',
    directives: [ROUTER_DIRECTIVES]
})
//AppComponent is the root of the application
export class NavbarComponent { 
    constructor() {

    }
}

navbar.component.html:

<nav class="navbar">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <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="#">Portfolio</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a a routerLink="/" routerLinkActive="active">Home <span class="sr-only">(current)</span></a></li>
                <li><a routerLink="/about" routerLinkActive="active">Over</a></li>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">C.V.</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a routerLink="/login" routerLinkActive="active">login</a></li>
                <li><a href="#">Register</a></li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>

app.routing.ts:

//import { Routes, RouterModule } from '@angular/router';
import { provideRouter, RouterConfig } from '@angular/router';
import { SearchComponent } from './components/search/search.component';
import { AboutComponent } from './components/about/about.component';
import { LoginComponent } from './components/login/login.component';

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

const appRoutes: RouterConfig = [
    {
        path: '', //represents the home page
        component: SearchComponent,
    },
    {
        path: 'about', 
        component: AboutComponent,
    },
    {
        path: 'login', 
        component: LoginComponent,
    }
];

export const appRouterProviders = [
    provideRouter(appRoutes)
];

main.ts:

//Angular's browser bootstrap function
import { bootstrap }    from '@angular/platform-browser-dynamic';
import { appRouterProviders } from './app.routing';

//The application root component, AppComponent
import { AppComponent } from './app.component';
bootstrap(AppComponent, [appRouterProviders]);

app.component.ts:

import { Component } from '@angular/core';
import { NavbarComponent } from './components/navbar/navbar.component';
import {ROUTER_DIRECTIVES} from '@angular/router';
import { SearchComponent } from './components/search/search.component';
import { AboutComponent } from './components/about/about.component';

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html',
    directives: [ROUTER_DIRECTIVES, NavbarComponent],
    //    precompile: [SearchComponent, AboutComponent]
})
//AppComponent is the root of the application
export class AppComponent {
    constructor() {

    }
}

app.component.html:

<navbar></navbar>
<div class="main">
    <div class="container">
        <router-outlet></router-outlet>
    </div>
</div>