简体   繁体   English

检测角度页面更改

[英]Detecting Angular page change

I am working with a third-party website that uses Angular. 我正在使用使用Angular的第三方网站。
I need to perform some actions when the user logs into the customer page, that means whenever the user goes from the log-in page, to his logged area. 当用户登录到客户页面时,我需要执行一些操作,这意味着每当用户从登录页面转到其登录区域时。 (Both are on the same website and use Angular). (两者都在同一个网站上并使用Angular)。
This used to work before, since the actions happen on page reload, but since we are in an SPA, the page reload does not happen when the page changes. 由于操作是在页面重新加载时发生的,因此以前可以使用,但是由于我们在SPA中,因此页面更改时不会发生页面重新加载。

My code lives on the page, but is not "inside" the Angular application and I have never used Angular before, so I only have visible to me the elements of the webpage that are global. 我的代码位于页面上,但不在Angular应用程序“内部”,并且我以前从未使用过Angular,因此我只看到全局的网页元素。

One way to solve the problem is to monitor the URL of the current page and detect when it changes from: https://somedomain/#/loginpage to https://somedomain/#/loggedArea using the window.location object: 解决此问题的一种方法是监视当前页面的URL,并使用window.location对象检测其何时从以下位置更改: https://somedomain/#/loginpage变为https://somedomain/#/loggedArea

 var previousHash; //This will store the hash of the page we were in before // Then we would have to constantly check if the URL has changed if(previousHash == "#/login" && window.location.hash == "#/loggedArea"){ callOnLoadActions() } 

but that seems a little brittle and hacky to me. 但这对我来说似乎有点脆弱和棘手。

Is there an "official", or at least better way of listening to when the page gets updated? 页面更新时是否有“官方”或至少更好的聆听方式?

Kind of an external solution to angular, but maybe using window.onhashchange will work in this scenario for you. 某种角度的外部解决方案,但也许可以使用window.onhashchange在这种情况下为您工作。

function YouAreGoingPlaces() {
    alert("Welcome to " + location.hash)
}
window.onhashchange = YouAreGoingPlaces() {
   your custom actions here.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM