我陷入一个非常简单的问题,不知道出了什么问题。当任何人点击用户组件上的值时,我想要路由到用户组件。附上下面的图片来检查这个。我也添加了用户路线,不确定是否无法接收
这是我的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 { HomeComponent } from './components/home/home.component';
import { AboutComponent } from './components/about/about.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { RegisterComponent } from './components/register/register.component';
import {AngularFireModule} from 'angularfire2';
import { UsersComponent } from './components/users/users.component';
import {FirebaseService} from './services/firebase.service';
import { AngularFireAuthModule ,AngularFireAuth} from 'angularfire2/auth';
import { AngularFireDatabaseModule,AngularFireDatabase,FirebaseListObservable } from "angularfire2/database-deprecated";
import { UserComponent } from './components/user/user.component';
const appRoutes:Routes=[
{
path:'' ,component:HomeComponent},
{path:'about',component:AboutComponent},
{path:'user/:id',component:UserComponent},
{path:'users',component:UsersComponent}
]
@NgModule({
declarations: [
AppComponent,
HomeComponent,
AboutComponent,
NavbarComponent,
RegisterComponent,
UsersComponent,
UserComponent,
],
imports: [
BrowserModule,
AngularFireModule.initializeApp(firebaseConfig),
RouterModule.forRoot(appRoutes),
AngularFireDatabaseModule,
AngularFireAuthModule
],
providers: [FirebaseService],
bootstrap: [AppComponent]
})
export class AppModule { }
这是我的users.component.ts
import { Component, OnInit } from '@angular/core';
import{FirebaseService} from '../../services/firebase.service';
import 'rxjs/Rx';
@Component({
selector: 'app-users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.css']
})
export class UsersComponent {
public users1:Array<Object>=[];
public valid:Boolean = false;;
constructor(private firebaseservice:FirebaseService) {
this.firebaseservice.getusers().subscribe(users1=>{
let tempuser= users1[0]
for(let Object in tempuser){
this.users1.push(tempuser[Object])
}
console.log(users1);
});
}
}
这是我的users.component.html代码&#39;&#39;
<ul class="list-group" >
<li class='list-group-item' *ngFor='let user of users1'>
<a routerLink="['/user/'+user.$key]"> {{user.city}} </a>
</li>
</ul>
这是我的firebase.service.ts类
import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from "angularfire2/database-deprecated";
@Injectable()
export class FirebaseService {
users:FirebaseListObservable<any[]>;
constructor(private af:AngularFireDatabase) { }
getusers()
{
this.users=this.af.list('/users') as FirebaseListObservable<Users[]>
return this.users;
}
}
interface Users{
$key?:string;
title?:string;
type?:string;
image?:string;
city?:string;
owner?:string;
bedrooms?:string;
path?:string;
price?:string;
}
数据图片 1