我正在运行两个检查,然后在满足两个条件的情况下有条件地将某些数据填充到某些路径。在我的room.component.html文件中,我正在使用* ngIf:
<div *ngIf="isLoggedIn() && isRoomRoute()" class="others">
... do some work
</div>
我的room.component.ts文件如下所示:
import { RouteService } from './../../data/route.service';
import { Component } from '@angular/core';
import { AuthenticationService } from './../../data/authentication.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-room',
templateUrl: './room.component.html',
styleUrls: ['./room.component.less']
})
export class RoomComponent {
model: any = {};
loading = false;
username;
password;
routeUrl;
private url = 'http://localhost:5000';
constructor(private authenticationService: AuthenticationService,
private router: Router,
private routeService: RouteService,
private appComponent: AppComponent) { }
isLoggedIn() {
this.loading = true;
if (this.authenticationService.isAuthenticated()) {
return true;
}
}
isRoomRoute(routeUrl) {
if (this.routeService.isRoomRoute(this.routeUrl)) {
return true;
}
}
}
如上所示,第二项检查是使用我的routeService中的函数。该功能如下所示:
isRoomRoute(routeUrl) {
if (routeUrl.includes('staff')) {
console.log('This url: ' + routeUrl + ' is a roomRoute');
return true;
} else {
console.log('This url: ' + routeUrl + ' is NOT a room route');
return false;
}
}
我的app.component正在跟踪这个网址,它在构造函数中使用了routeService,如下所示:
constructor(private routeService: RouteService,
private router: Router)
{
this.router.events.subscribe((route) => {
let routeUrl = route.url;
this.routeService.sendRoute(routeUrl);
this.routeService.isRoomRoute(routeUrl);
});
}
我从routerService的“isRoomRoute”函数中成功获得了正确的结果,但是当我尝试将结果传递给我的房间组件时,我收到“未定义”错误,即使我在调用routeService时零件。所以我的问题是,我错过了什么让房间组件的结果“未定义”?如何将routeService中isRoomRoute()函数的结果中的布尔值传递给我的房间组件?