angular - ActivatedRoute 未定义自定义按钮单击
问题描述
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
可用?
解决方案
尝试替换:
function(){
alert(this._route);//undefined
this._route.navigate(["/addPage"]);
}
和:
() => {
alert(this._route);//undefined
this._route.navigate(["/addPage"]);
}
否则this
指作为navButtonAdd
函数的第二个参数传递的对象
推荐阅读
- php - 异常无法解析时间字符串
- ios - Dispatch Semaphore 会无意中死锁吗?
- shadow-dom - shadowRoot.getSelection()?
- python - 我无法显示评论数
- java - 为什么我的 do-while 循环在使用 Java Scanner 输入无效输入后变成无限循环?
- reactjs - 文档 Firebase React 上的 onSnapshot
- c++ - 使用中继器时不显示 Qml 按钮列表
- python - 如何通过python脚本将信息从mysql数据库发送到ios应用程序
- flutter - Flutter Stream 每次应用启动时仅工作一次
- reactjs - Material UI Switch 自定义类 react