代码之家  ›  专栏  ›  技术社区  ›  amdg

为自定义按钮单击未定义的ActivateRoute

  •  0
  • amdg  · 技术社区  · 8 年前

    下面的代码实例化 jqgrid 当columnModel可用时,还有一个自定义按钮。这很管用。

        @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 回复  |  直到 8 年前
        1
  •  1
  •   Guerric P    8 年前

    尝试替换:

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

    使用:

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

    this 引用作为 navButtonAdd 功能

    推荐文章