【发布时间】:2016-12-18 08:10:35
【问题描述】:
我无法单击<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");
}
}
我是做错了什么还是我错过了什么?无论如何感谢您的回复。
【问题讨论】:
-
为什么你的登录组件中有一个选择器?这个项目还有比显示的更多吗?你的“index.html”也是一个相当可怕的野兽。我认为您不应该将路由器链接嵌套在其中。相反,它应该在导航栏之类的组件内。尝试将该路由器链接复制到您的 app.component (home.html) 中,看看您是否可以导航。
-
@Bean0341 我还在忙。我现在已经在我的登录组件中删除了该选择器。而且我已经尝试过了,我仍然无法点击
<a routerLink="/login">Login</a> -
您是否也尝试过在组件内移动路由器插座?
-
@Bean0341 不,因为我不希望我的 HTML 与组件分离。
-
我的问题几乎相同,只是想知道在使用 Angular 2
routerLink时如何在我的活动链接中包含<span class="sr-only">(current)</span>?
标签: html angular typescript routing