如何在角度分量中设置onhashchange

时间:2017-09-28 06:23:35

标签: angular lifecycle

我无法使用angular 4设置 onhashchange 属性,我尝试在角度4的以下生命周期中设置onhashchange,但它无效。

ngAfterContentInit

  ngAfterContentInit() {
    //disabling browser back button
    window.location.hash = "lienholder-details";
    window.location.hash = "Again-lienholder-details";//again because google chrome don't insert first hash into history
    window.onhashchange = function () { window.location.hash = "lienholder-details"; }
  }

ngOnInit

ngOnInit(){
 //disabling browser back button
        window.location.hash = "lienholder-details";
        window.location.hash = "Again-lienholder-details";//again because google chrome don't insert first hash into history
        window.onhashchange = function () { window.location.hash = "lienholder-details"; }
}

如何在angular 4中设置onhashchange属性

2 个答案:

答案 0 :(得分:1)

//View curr = parent.getChildAt((int) id);
View curr = parent.getChildAt(position);
//I'm getting an error here.
TextView c = (TextView) curr.findViewById(R.id.gorevNo);

或者如果您使用的是路由器

@HostListener('window:hashchange', ['$event'])
hashChangeHandler(e) {
  window.location.hash = "lienholder-details";
}

(另见https://stackoverflow.com/a/38080657/217408

答案 1 :(得分:0)

我使用了ngAfterViewChecked事件,我可以设置onhashchange属性