Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检测角度按下的后退按钮2_Javascript_Angularjs_Angular - Fatal编程技术网

Javascript 检测角度按下的后退按钮2

Javascript 检测角度按下的后退按钮2,javascript,angularjs,angular,Javascript,Angularjs,Angular,我试图检测加载此组件时是否按下了后退按钮。在ngOnInit()中,我想知道是否单击了“上一步”,因此我不会清除所有过滤器。代码如下: export class ProductListComponent implements OnInit, OnDestroy { constructor (private _productsService: ProductsService, params: RouteParams, private _categoriesService: CategoriesSe

我试图检测加载此组件时是否按下了后退按钮。在ngOnInit()中,我想知道是否单击了“上一步”,因此我不会清除所有过滤器。代码如下:

export class ProductListComponent implements OnInit, OnDestroy {
constructor (private _productsService: ProductsService, params: RouteParams, private _categoriesService: CategoriesService, private _filtersService: FiltersService, private _router: Router, private _location: Location) {
    this.category = params.get('name') ? params.get('name') : null;
}

subscription: any;
category: any;
loading: boolean = false;
page: number = 1;
count: number;
products: any;
pages: Array = [];
errorMessage: string;

ngOnInit() {

    this.getProducts();

    //if(back button wasnt used) {
    //    this._filtersService.clear();
    //}

    this.subscription = this._filtersService.filterUpdate.subscribe(
        (filters) => {
            this.page = 1;
            var params = this.category ? {name: this.category} : {};
            this._router.navigate([this.currentRoute, params]);
            this.getProducts();
        }
    );
}

在服务中创建一个变量,将其初始化为false,并在ngOnInit方法中将其设置为true。由于服务只初始化一次,请在ngOnInit中添加:-

 ngOnInit(){
    if(!this._filterService.flag) this._filterService.clear()
  this._filterService.flag = true;
  //rest of your code
 }

确保服务只初始化一次,即应用程序启动时,而不是在组件的元数据内。如果刷新页面,则标志值最初将为false,如果使用“后退”按钮访问组件,则标志值将为true,在ngOnInit方法内将其初始化为false并将其设置为true。由于服务仅初始化一次,请在ngOnInit中添加:-

 ngOnInit(){
    if(!this._filterService.flag) this._filterService.clear()
  this._filterService.flag = true;
  //rest of your code
 }

确保服务只初始化一次,即应用程序启动时,而不是在组件的元数据中。如果页面刷新,如果使用后退按钮访问组件,则标志值最初将为false,如果使用后退按钮访问组件,则标志值将为true。在我的应用程序中,我创建了一个
NavigationService
,其中包含一个可用于确定是否按下后退按钮的标志

从'@angular/core'导入{Injectable};
从'@angular/Router'导入{Router,NavigationStart};
从“rxjs/operators”导入{tap、filter、pairwise、startWith、map};
@注射的({
providedIn:'根'
})
导出类导航服务{
wasBackButtonPressed=false;
构造函数(专用路由器:路由器){
这是.\u router.events.pipe(
过滤器(ev=>导航启动的ev实例),
地图(ev=>ev),
startWith(空),
成对(),
抽头([ev1,ev2])=>{
如果(!ev1 | |(ev2.url!==ev1.url)){
this.wasBackButtonPressed=ev2.navigationTrigger=='popstate';
}
})
).subscribe();
}
}
它使用Rxjs
pairwise()
操作符,因为我注意到后退按钮会导致
NavigationStart
事件被发送两次,第一次是
navigationTrigger='popstate'
,这就是我们要寻找的

现在,我可以将此服务注入到我的组件中,并且可以引用此标志来确定如果用户通过浏览器的“后退”按钮到达,是否运行特殊逻辑

import { Component, OnInit } from '@angular/core';
import { NavigationService } from 'src/services/navigation.service';

@Component({
    selector: 'app-example',
    templateUrl: './app-example.component.html',
    styleUrls: ['./app-example.component.scss']
})
export class ExampleComponent implements OnInit {

    constructor(private _navigationService: NavigationService) {
    }

    ngOnInit(): void {
        if (this._navigationService.wasBackButtonPressed) {
            // special logic here when user navigated via back button
        }
    }
}

另一件需要知道的事情是,这个
NavigationService
应该在应用程序启动时立即运行,这样它就可以开始进行第一次路线更改。要做到这一点,请将其注入根
app.component

在我的应用程序中,我创建了一个
NavigationService
,其中包含一个标志,可用于确定是否按下了后退按钮

从'@angular/core'导入{Injectable};
从'@angular/Router'导入{Router,NavigationStart};
从“rxjs/operators”导入{tap、filter、pairwise、startWith、map};
@注射的({
providedIn:'根'
})
导出类导航服务{
wasBackButtonPressed=false;
构造函数(专用路由器:路由器){
这是.\u router.events.pipe(
过滤器(ev=>导航启动的ev实例),
地图(ev=>ev),
startWith(空),
成对(),
抽头([ev1,ev2])=>{
如果(!ev1 | |(ev2.url!==ev1.url)){
this.wasBackButtonPressed=ev2.navigationTrigger=='popstate';
}
})
).subscribe();
}
}
它使用Rxjs
pairwise()
操作符,因为我注意到后退按钮会导致
NavigationStart
事件被发送两次,第一次是
navigationTrigger='popstate'
,这就是我们要寻找的

现在,我可以将此服务注入到我的组件中,并且可以引用此标志来确定如果用户通过浏览器的“后退”按钮到达,是否运行特殊逻辑

import { Component, OnInit } from '@angular/core';
import { NavigationService } from 'src/services/navigation.service';

@Component({
    selector: 'app-example',
    templateUrl: './app-example.component.html',
    styleUrls: ['./app-example.component.scss']
})
export class ExampleComponent implements OnInit {

    constructor(private _navigationService: NavigationService) {
    }

    ngOnInit(): void {
        if (this._navigationService.wasBackButtonPressed) {
            // special logic here when user navigated via back button
        }
    }
}

另一件需要知道的事情是,这个
NavigationService
应该在应用程序启动时立即运行,这样它就可以开始进行第一次路线更改。要做到这一点,请将其注入根
app.component

on keydown事件不是一个选项吗?我需要检测它是否回叫on keydown事件不是一个选项吗?我需要检测它是否回叫这不是我所需要的,我需要知道什么时候按下了Back按钮,所以我不调用该维修行动。否则称之为“这不完全是我需要的”,我需要知道何时按下“后退”按钮,这样我就不会称之为“维修行动”。否则就叫它