我是 Angular 的新手(今天 18 年 3 月 9 日是第 8 天)。
我有一个需要在窗口加载事件上执行的 jquery 方法。this.router.navigate(['/student-consent'])
当我这样做或单击浏览器上的导航按钮以访问 url窗口加载事件时,从我的角度组件 /student-consent
没有触发,因此我的方法没有运行。
但是,当我这样做时 window.location.href = '/student-consent'
,我的方法正在运行。
我对此进行了一些研究,我发现在 Angular 路由导航或浏览器导航(在 Angular 应用程序中使用路由)不加载窗口,而是在路由器插座中加载相关的 html(组件)。
相反,刷新页面或直接在浏览器中点击 url 会执行这些情况下确实发生窗口加载事件的方法。
我的问题:有什么方法可以在路由更改时执行我的方法或在路由更改时触发窗口加载事件?
更新:根据设计约束,在我的组织中不允许使用 onAfterViewInit 或任何组件生命周期挂钩执行 jquery 方法。
js中的窗口加载事件处理程序 -
$(window).on("load", function () {
$(".preloader").fadeOut(1000);
});
我的组件中的相关 Html 代码 -
<div class="preloader"></div>
零件 -
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import * as $ from 'jquery';
@Component({
selector: 'app-student-login-creation',
templateUrl: './student-login-creation.component.html',
styleUrls: ['./student-login-creation.component.css']
})
export class StudentLoginCreationComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
//$(window).trigger('routeChange');
}
createId() {
console.log('student login');
// window.location.href = '/student-consent';
this.router.navigate(['/student-consent']);
}
}