对于自定义按钮单击,ActivatedRoute未定义

时间:2018-08-20 10:02:37

标签: angular jqgrid

columnModel可用后,以下代码将实例化jqgrid和一个自定义按钮。效果很好。

    @Component({
       selector: 'somecomp',
       templateUrl: './somecomp.component.html'
    })
    export class SomeComponent implements OnInit{
      private _someService;
      private _route;
      constructor(someService:SomeService,route:ActivatedRoute){ 
         this._someService = someService;
         this._route = route;  
      }

      ngOnInit(){
         this.someService.getColumnModel.subscribe(columnModel=>{

            (<any>jQuery('#grid')).jqGrid({
               colModel : columnModel,
               caption : 'Data Grid',
               url : "http://sampleurl.com/data"
            });
            //custom button
            (<any>jQuery('#grid')).navButtonAdd("#pager",{
                buttonicon:"ui-icon-add",
                onClickButton : () => {
                   alert(this._route);//Route(...)
                   this._route.navigate(["/addPage"]); 
                }   
            });
...

单击此按钮后,需要导航到另一个页面,如图所示。但是,该警报为未定义状态,错误为Cannot read property 'navigate' of undefined

如何确保this._route可用?

1 个答案:

答案 0 :(得分:1)

尝试替换:

function(){
   alert(this._route);//undefined
   this._route.navigate(["/addPage"]); 
}

具有:

() => {
   alert(this._route);//undefined
   this._route.navigate(["/addPage"]); 
}

否则,this是指作为navButtonAdd函数的第二个参数传递的对象

相关问题