Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 分配的变量是未定义的角度类型脚本_Angular_Typescript_Angular Router - Fatal编程技术网

Angular 分配的变量是未定义的角度类型脚本

Angular 分配的变量是未定义的角度类型脚本,angular,typescript,angular-router,Angular,Typescript,Angular Router,我有如下代码。构造函数使用router方法获取上一个路由,并在其中设置一个变量。然后ngOnInit()调用一个方法,该方法反过来调用另一个方法,将通过构造函数保存的变量与服务中的列表值进行比较。在这一点上,当比较存储在contructor中的变量时,总是显示为未定义。但是在构造函数中,this.selBoard的值为40。控制台中为“console.log(this.selBoard);”打印的值我总是没有定义。我哪里做错了?即使它是一个同步调用,值也被正确地打印出来,在构造函数中为40,因此

我有如下代码。构造函数使用router方法获取上一个路由,并在其中设置一个变量。然后ngOnInit()调用一个方法,该方法反过来调用另一个方法,将通过构造函数保存的变量与服务中的列表值进行比较。在这一点上,当比较存储在contructor中的变量时,总是显示为未定义。但是在构造函数中,this.selBoard的值为40。控制台中为“console.log(this.selBoard);”打印的值我总是没有定义。我哪里做错了?即使它是一个同步调用,值也被正确地打印出来,在构造函数中为40,因此在getRouterDetails()中它的值不可能是未定义的

/。。。
selBoard:编号;
所选板:板=新板();
构造函数(专用路由器:路由器){
this.router.events.pipe(过滤器((evt:any)=>evt-instanceof-routesRecognited),成对()
.subscribe((事件:RoutesRecognited[])=>{
var url=events[0]。urlAfterRedirects;
var urleindex=url.indexOf('boardId=');
var boardId=url.substring(urleindex+8,urleindex+10);
this.selBoard=编号(boardId);
console.log('selBoard--'+this.selBoard);
});
ngOnInit():void{
//..
if(this.loggedInUser.isAdmin){
this.loadBoardsList();}
..//
}
loadBoardsList(){
此._boardService.loadBoards().订阅(posts=>{
该数据=员额;
console.log('loadBoardsList',this.data);
},
错误=>{
log('loadBoardsList-error',error);
//此.u errorService.handleError(错误);
此._messageService.add({严重性:'error',摘要:'error Message',详细信息:error.error+'-'+error.Message,粘性:true});
}, 
() => {  
this.boardsList=this.data.boardLi;
这个.getRouterDetails();
console.log(此.selectedBoard);
});  
}
getRouterDetails(){
for(设i=0;i

非常感谢您的建议。谢谢。

您似乎正在以一种极其复杂的方式获取参数值。为什么不直接使用此.route.queryParams

constructor(private route: ActivatedRoute) { 
  this.route.queryParams.subscribe(params => {
    this.boardId = params.boardId;
  });
}

您必须记住,
.subscribe
是异步的,如果
ngOnInit(loadBoardList())
中的
.subscribe
发生在构造函数的
.subscribe
之前,那么您将面临此竞争条件(属性在某个地方未定义,在其他地方填充)。这些订阅似乎相互依赖,为此,我将使用
switchMap

import { switchMap } from 'rxjs/operators';
...
selBoard : number;
selectedBoard : Board= new Board();

    constructor(private router: Router){
      // get rid of the code inside of the constructor
    }
            
    ngOnInit(): void {
    //..
    if(this.loggedInUser.isAdmin)  {
        this.loadBoardsList();}
    ..//
    }
    
    
    loadBoardsList(){
        this.router.events.pipe(
            filter((evt: any) => evt instanceof RoutesRecognized), 
            pairwise(),
            switchMap((events: RoutesRecognized[]) => {
               var url = events[0].urlAfterRedirects;
               var urlIndex = url.indexOf('boardId=');
               var boardId = url.substring(urlIndex+8, urlIndex+10);
               this.selBoard = Number(boardId);
               console.log('selBoard --- '+this.selBoard);
               return this._boardService.loadBoards(); // switch to this observable now, once the previous is done
            }),    
        ).subscribe( posts =>{
                this.data = posts; 
                console.log('loadBoardsList',this.data);
                },
                error => { 
                console.log('loadBoardsList - error',error);
               // this._errorService.handleError(error); 
                this._messageService.add({severity:'error', summary:'Error Message', detail:error.error+' - '+error.message, sticky: true}); 
                }, 
                () => {  
                this.boardsList = this.data.boardLi; 
                this.getRouterDetails();
                console.log(this.selectedBoard);
            });  
    }
    
    getRouterDetails(){
       for (let i = 0; i < this.boardsList.length; i++) {
            console.log(this.boardsList[i].id);
            console.log(this.selBoard);
            if(this.boardsList[i].id == this.selBoard){
                console.log('Which one selected ---'+this.selBoard);
                this.selectedBoard = this.boardsList[i];
            }
        }
     }
从'rxjs/operators'导入{switchMap};
...
selBoard:编号;
所选板:板=新板();
构造函数(专用路由器:路由器){
//去掉构造函数内部的代码
}
ngOnInit():void{
//..
if(this.loggedInUser.isAdmin){
this.loadBoardsList();}
..//
}
装载板列表(){
此为.router.events.pipe(
过滤器((evt:any)=>已识别路由的evt实例),
成对(),
switchMap((事件:RoutesRecognited[])=>{
var url=events[0]。urlAfterRedirects;
var urleindex=url.indexOf('boardId=');
var boardId=url.substring(urleindex+8,urleindex+10);
this.selBoard=编号(boardId);
console.log('selBoard--'+this.selBoard);
返回此项。_boardService.loadBoards();//完成上一项后,立即切换到此可观察项
}),    
).订阅(帖子=>{
该数据=员额;
console.log('loadBoardsList',this.data);
},
错误=>{
log('loadBoardsList-error',error);
//此.u errorService.handleError(错误);
此._messageService.add({严重性:'error',摘要:'error Message',详细信息:error.error+'-'+error.Message,粘性:true});
}, 
() => {  
this.boardsList=this.data.boardLi;
这个.getRouterDetails();
console.log(此.selectedBoard);
});  
}
getRouterDetails(){
for(设i=0;i
感谢您的回复。我没有获取当前的url。我获取的是以前的url,这就是它的工作方式。如果您有一个简单的方法获取以前的路线,请让我知道。例如,我单击A,当我单击B时,我应该获取A的链接。嗯,我想我仍然不明白。如果您可以访问A上的boardId,为什么不直接访问当您单击B时,将其作为参数传递?好的,这就是我的页面。假设a、B、C作为父菜单,当我单击a时,我会看到x、y、z页面。因此,从a开始,boardId作为参数传递。例如,当我在z中,当我现在单击a时,我希望保留boardId。当我在x、y或z中,单击B或C时,boardId应该为空。一种方法是要识别上一条路由,如果它是x、y或z,则获取boardId else null。但是,当路由器使用参数值进行导航时,会传递此消息。子页面
import { switchMap } from 'rxjs/operators';
...
selBoard : number;
selectedBoard : Board= new Board();

    constructor(private router: Router){
      // get rid of the code inside of the constructor
    }
            
    ngOnInit(): void {
    //..
    if(this.loggedInUser.isAdmin)  {
        this.loadBoardsList();}
    ..//
    }
    
    
    loadBoardsList(){
        this.router.events.pipe(
            filter((evt: any) => evt instanceof RoutesRecognized), 
            pairwise(),
            switchMap((events: RoutesRecognized[]) => {
               var url = events[0].urlAfterRedirects;
               var urlIndex = url.indexOf('boardId=');
               var boardId = url.substring(urlIndex+8, urlIndex+10);
               this.selBoard = Number(boardId);
               console.log('selBoard --- '+this.selBoard);
               return this._boardService.loadBoards(); // switch to this observable now, once the previous is done
            }),    
        ).subscribe( posts =>{
                this.data = posts; 
                console.log('loadBoardsList',this.data);
                },
                error => { 
                console.log('loadBoardsList - error',error);
               // this._errorService.handleError(error); 
                this._messageService.add({severity:'error', summary:'Error Message', detail:error.error+' - '+error.message, sticky: true}); 
                }, 
                () => {  
                this.boardsList = this.data.boardLi; 
                this.getRouterDetails();
                console.log(this.selectedBoard);
            });  
    }
    
    getRouterDetails(){
       for (let i = 0; i < this.boardsList.length; i++) {
            console.log(this.boardsList[i].id);
            console.log(this.selBoard);
            if(this.boardsList[i].id == this.selBoard){
                console.log('Which one selected ---'+this.selBoard);
                this.selectedBoard = this.boardsList[i];
            }
        }
     }