我在传递actionmenuline的值时遇到问题。 actionmenuline值传入的是不确定的,我不确定发生了什么。
服务正在为第一个值而不是第二个值传递正确的代码。我想知道我在getAction调用中是否做错了什么,没有得到正确的对象。
这是我得到的错误:->
无法读取未定义的属性“ subscribe” 在新的ActionViewComponent(actionview.component.ts:68)上,第68行指向this.actionMenuSubscription
操作视图组件
export class ActionViewComponent implements OnInit, OnDestroy {
// User Fields
currentUser: User;
users: User[] = [];
currentUserSubscription: Subscription;
// Menu Lines
currentActionMenuLine: ActionMenuLine;
actionMenuLines: ActionMenuLine[] = [];
actionMenuSubscription: Subscription;
dataSource: any = new MatTableDataSource(ACTION_DATA);
constructor(
private authenticationService: AuthenticationService,
private actionService: ActionService,
private menuService: MenuService,
) {
this.currentUserSubscription = this.authenticationService.currentUser.subscribe(user => {
this.currentUser = user;
});
this.actionMenuSubscription = this.menuService.currentActionMenuLine.subscribe(actionmenuline => {
this.currentActionMenuLine = actionmenuline;
});
}
public getActions() {
this.actionService.getAction(this.currentUser, this.currentActionMenuLine).
subscribe((data: any) => {
this.dataSource = data;
});
}
}
行动服务
export class ActionService {
public apiURL = 'http://localhost:15217/api/actions/launchaction';
public currentUser: Observable<User>;
public currentAction: Observable<Action>;
private currentActionSubject: BehaviorSubject<Action>;
public ActionMenuLine: Observable<ActionMenuLine>;
constructor(private http: HttpClient) {
this.currentActionSubject = new BehaviorSubject<Action>(JSON.parse(localStorage.getItem('currentAction')));
this.currentAction = this.currentActionSubject.asObservable();
}
// Http Options
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
getAction( user: User, actionmenuline: ActionMenuLine) {
return this.http.post<User[]>(this.apiURL,
{
SessionID: user.userEnv.sessionId,
ActionTag: actionmenuline.targetActionTag
})
.pipe(
retry(1),
catchError(this.handleError)
);
}
菜单服务
export class MenuService {
public currentUser: Observable<User>;
private currentActionMenuLineSubject: BehaviorSubject<ActionMenuLine>;
public currentActionMenuLine: Observable<ActionMenuLine>;
ActionMenuSubscription: any;
constructor(private http: HttpClient) { }
loadActionMenu(user: User): Observable<ActionMenuLine[]> {
return this.http.get<ActionMenuLine[]>(`/menu/getlines/`, {
params : {
userId: user.id,
sessionId: user.userEnv.sessionId
}
}).pipe(map( MenuLines => {
return this.debugLines(MenuLines) as ActionMenuLine[];
}));
}
debugLines(lines: ActionMenuLine[]){
let i:number = 0;
let kids:ActionMenuLine[];
for( let i:number = 0; i< lines.length; i++ ){
let j:ActionMenuLine = lines[i];
kids = j.children;
}
return lines;
}
loadAboutSyntelic():Observable<string[]> {
return this.http.get<string[]>('usersettings/aboutsyntelic/').pipe(map( AboutSyntelic => {
return AboutSyntelic as string[];
}));
}
loadMimic(sessionId: string):Observable<string[]> {
return this.http.get<string[]>("usersettings/mimicdata/",{
params: {
sessionId: sessionId
}
}).pipe(map(ids => ids as string[]));
}
mimic(userId: string, sessionId: string){
return this.http.get<any>("usersettings/mimic/",{
params : {
userId: userId,
sessionId: sessionId
}
}).pipe(map( res => {
return res as User
}));
}
答案 0 :(得分:2)
currentActionMenuLine
在 MenuService
中定义,但在整个类中均未分配值。
MenuService.ts
public currentActionMenuLine$: Observable<ActionMenuLine>;
服务正在为第一个值而不是第二个值传递正确的代码。
那是因为也许您正在 AuthenticationService
中为其分配一个值,但问题中没有出现。
检查您的 AuthenticationService 类,然后在此处为currentActionMenuLine
执行相同操作(添加初始值)。
PS:
通常,我们在Observable变量名称的末尾使用
$
符号。
答案 1 :(得分:0)
将服务修改为
getAction( user: User, actionmenuline: ActionMenuLine): Observable<any> {
return this.http.post<User[]>(this.apiURL,
{
SessionID: user.userEnv.sessionId,
ActionTag: actionmenuline.targetActionTag
})
}
在组件中以及组件中使用它而不是在服务中添加管道
public getActions() {
this.actionService.getAction(this.currentUser, this.currentActionMenuLine)
.pipe(retry(1))
.subscribe((data: any) => {
this.dataSource = data;
});
}
答案 2 :(得分:0)
在currentActionMenuLine
中没有用于初始化menuService
的代码。您已将其键入为Observable
,但由于该值仍为undefined
,因此在运行时不会起作用。