2016-08-11 117 views
2

我無法點擊<a routerLink="/login">Login</a>從主頁導航到登錄頁面。我已經閱讀教程和角2Angular 2 routerLink不起作用

網站上的開發人員指南,這是我在我的代碼:

的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"); 
    } 
} 

我做得不對還是我失去了什麼?無論如何感謝您的回覆。

+0

爲什麼你在你的登錄組件選擇?這個項目是否比展示的更多?你的「index.html」也是一個相當可怕的野獸。我不認爲你應該有你的路由器鏈接嵌套在那裏。相反,它應該在像導航欄這樣的組件內。嘗試將該路由器鏈接複製到app.component(home.html)中,然後查看是否可以導航。 – Bean0341

+0

@ Bean0341我還是很忙。我現在已經在登錄組件中刪除了該選擇器。我已經嘗試過了,我仍然無法點擊'Login' – superkytoz

+0

您是否還嘗試過移動組件內的路由器插座? – Bean0341

回答

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>