在 Javascript 代码中访问路由器
Access Router in Javascript code
我有一个带有 JS 函数的 TypeScript 组件。该函数由 Kendo UI 调用,因此该函数在 Javascript 上下文中执行,这意味着我不能使用 "this" (这个 returns 调用者 Kendo组件)。我想在此函数中重定向页面,但我宁愿使用 router.navigate* 函数而不是 location.href 替换。是否可以在 Javascript 上下文中访问路由器实例?
export class MyComponent implements AfterViewInit {
//template: `<div #grid></div>`
@ViewChild('grid') private grid: ElementRef;
ngAfterViewInit() {
this.configureGrid();
}
private configureGrid() {
let kendoGrid: kendo.ui.Grid = jQuery(this.grid.nativeElement).data("kendoGrid");
let kendoColumn: kendo.ui.GridColumn = {};
kendoColumn.command = { text: "My Button", click: this.myButtonCommand, className: "btn" };
kendoGrid.columns.add(kendoColumn);
}
myButtonCommand(e) {
//redirect here
}
}
我认为这应该可行
private configureGrid() {
let kendoGrid: kendo.ui.Grid = jQuery(this.grid.nativeElement).data("kendoGrid");
let kendoColumn: kendo.ui.GridColumn = {};
kendoColumn.command = { text: "My Button", click: (e) => this.myButtonCommand(e), className: "btn" };
kendoGrid.columns.add(kendoColumn);
}
你应该可以在 myButtonCommand()
中调用 this.router.navigate...()
我有一个带有 JS 函数的 TypeScript 组件。该函数由 Kendo UI 调用,因此该函数在 Javascript 上下文中执行,这意味着我不能使用 "this" (这个 returns 调用者 Kendo组件)。我想在此函数中重定向页面,但我宁愿使用 router.navigate* 函数而不是 location.href 替换。是否可以在 Javascript 上下文中访问路由器实例?
export class MyComponent implements AfterViewInit {
//template: `<div #grid></div>`
@ViewChild('grid') private grid: ElementRef;
ngAfterViewInit() {
this.configureGrid();
}
private configureGrid() {
let kendoGrid: kendo.ui.Grid = jQuery(this.grid.nativeElement).data("kendoGrid");
let kendoColumn: kendo.ui.GridColumn = {};
kendoColumn.command = { text: "My Button", click: this.myButtonCommand, className: "btn" };
kendoGrid.columns.add(kendoColumn);
}
myButtonCommand(e) {
//redirect here
}
}
我认为这应该可行
private configureGrid() {
let kendoGrid: kendo.ui.Grid = jQuery(this.grid.nativeElement).data("kendoGrid");
let kendoColumn: kendo.ui.GridColumn = {};
kendoColumn.command = { text: "My Button", click: (e) => this.myButtonCommand(e), className: "btn" };
kendoGrid.columns.add(kendoColumn);
}
你应该可以在 myButtonCommand()
this.router.navigate...()