RouterLink在Angular 6中不起作用 [英] RouterLink is not working in Angular 6

查看:404
本文介绍了RouterLink在Angular 6中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在看Brad Traversy教程。我完全按照说的做。
这是我的 app.module.ts。

I was coding watching a Brad Traversy tutorial. and I did exactly as it is said. this is my 'app.module.ts'.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {RouterModule, Routes} from '@angular/router';

import { AppComponent } from './app.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { LoginComponent } from './components/login/login.component';
import { RegisterComponent } from './components/register/register.component';
import { HomeComponent } from './components/home/home.component';
import { ProfileComponent } from './components/profile/profile.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';

const appRoutes: Routes = [
  {path:'', component: HomeComponent},
  {path:'register', component: RegisterComponent},
  {path:'login', component: LoginComponent},
  {path:'dashboard', component: DashboardComponent},
  {path:'profile', component: ProfileComponent}
] 
@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent,
    LoginComponent,
    RegisterComponent,
    HomeComponent,
    ProfileComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot(appRoutes) // appRoutes: an object

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是我的导航栏组件

 <li><a class="nav-link" [routerLink] = "['/']" [routerLinkActive]=" 
['active']">Home</a></li>
 <li><a class="nav-link" [routerLink] = "['/login']" [routerLinkActive]=" 
['active']">Login</a></li>

我还添加了

<base href="/">

到index.html文件。

to the index.html file.

当我删除RouterLink部件时,页面运行良好。当我在URL中提供路径时,它将显示组件内部的内容。

When I remove the RouterLink part the pages are working good.it displays the content inside the components when I give the path in the URL.

我检查了几个问题。

推荐答案

将routerLinkActive写为波纹管。.去掉那些括号 p>

write routerLinkActive as bellow..remove those brackets

 <li>
    <a class="nav-link" [routerLink] = "['/']" routerLinkActive="active">Home</a>
    </li>
    <li>
    <a class="nav-link" [routerLink] = "['/login']" routerLinkActive="active">Login</a> 
   </li>

官方文档:- https://angular.io/guide/router

这篇关于RouterLink在Angular 6中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆