我正在使用TypeScript学习Angular JS。我有Angular JS的经验,但是当它与TypeScript集成时,对我来说完全是新的。我现在正在为我的应用程序配置路由。
然后我点击链接。两个链接都完全不起作用。但是当我直接在URL栏中输入路由值进行访问时,它可以工作。我的HomeComponent和DirectoryComponent很简单。它们只是显示一条消息。
当我首次访问“/directory”网址时,它可以工作。它显示目录页面。从那里,当我点击home链接时,它会更新到主页。但是URL仍然是“directory”。但是当我再次点击目录链接时,什么都没有发生。
我遵循了这些解决方案 routerLink is not working in angular 2 routerlink not working in angular2 RouterLink does not work 所有的都不起作用。
我检查了HTML元素,它正确地呈现了链接。问题在于我点击链接时什么都没有发生。 我正在使用最新版本的Angular JS。我的代码有什么问题?
This is my app.module.ts file
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { DirectoryComponent } from './directory/directory.component';
//import { AppRoutingModule } from './app-routing.module';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path : 'directory',
component : DirectoryComponent
}
];
@NgModule({
declarations: [
AppComponent,
HomeComponent,
DirectoryComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(routes)
],
providers: [ ],
bootstrap: [AppComponent]
})
export class AppModule { }
我在那个文件中配置了路由。当我直接从浏览器访问路由时,它可以正常工作。然后我尝试在app.component.html中添加链接。 这是我的app.component.html
<h1>
{{title}}
</h1>
<div>
<ul>
<li>
<a [routerLink]="['/']">Home</a>
</li>
<li>
<a [routerLink]="['directory']">Directory</a>
</li>
</ul>
</div>
<div>
<router-outlet></router-outlet>
</div>
然后我点击链接。两个链接都完全不起作用。但是当我直接在URL栏中输入路由值进行访问时,它可以工作。我的HomeComponent和DirectoryComponent很简单。它们只是显示一条消息。
当我首次访问“/directory”网址时,它可以工作。它显示目录页面。从那里,当我点击home链接时,它会更新到主页。但是URL仍然是“directory”。但是当我再次点击目录链接时,什么都没有发生。
我遵循了这些解决方案 routerLink is not working in angular 2 routerlink not working in angular2 RouterLink does not work 所有的都不起作用。
我检查了HTML元素,它正确地呈现了链接。问题在于我点击链接时什么都没有发生。 我正在使用最新版本的Angular JS。我的代码有什么问题?