我在 Angular 6 中使用 Bootstrap 4 模态,我想在模态关闭后将用户重定向到另一条路线。但是,当模式关闭时,我遇到了范围界定问题,告诉我注入(inject)的路由器未定义。
我的代码:
import { Component, OnInit } from '@angular/core';
declare var $: any
@Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit {
constructor(
public router: Router
) {}
ngOnInit() {}
ngAfterViewInit() {
$('#mymodal').on('hidden.bs.modal', function (e) {
router.navigate(['/']) //tells me that router is undefined
})
}
}
最佳答案
像 Jquery
中那样使用 this
关键字。
ngAfterViewInit() {
var self = this;
$('#mymodal').on('hidden.bs.modal', function (e) {
self.router.navigate(['/']);
})
}
关于javascript - 在 Angular 中的 jQuery 事件监听器中使用注入(inject)的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52357078/