【问题标题】:Angular 2 routerLink doesn't workAngular 2 routerLink 不起作用
【发布时间】: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 我还在忙。我现在已经在我的登录组件中删除了该选择器。而且我已经尝试过了,我仍然无法点击&lt;a routerLink="/login"&gt;Login&lt;/a&gt;
  • 您是否也尝试过在组件内移动路由器插座?
  • @Bean0341 不,因为我不希望我的 HTML 与组件分离。
  • 我的问题几乎相同,只是想知道在使用 Angular 2 routerLink 时如何在我的活动链接中包含 &lt;span class="sr-only"&gt;(current)&lt;/span&gt;

标签: html angular typescript routing


【解决方案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>

【讨论】:

    猜你喜欢
    • 2020-01-30
    • 2018-06-06
    • 1970-01-01
    • 2016-12-15
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多