Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
Ionic framework Ionic 4在维修/防护装置中的滚动位置_Ionic Framework_Ionic4 - Fatal编程技术网

Ionic framework Ionic 4在维修/防护装置中的滚动位置

Ionic framework Ionic 4在维修/防护装置中的滚动位置,ionic-framework,ionic4,Ionic Framework,Ionic4,我正在尝试实现一个类似于Facebook的功能,也就是说,如果你滚动了新闻提要,按下硬件后退按钮,你就会进入列表的顶部 为此,我认为canDeactivate的Router-Guards将是正确的方法 但是我无法找到一种方法来检查页面是否被滚动 我尝试了window.pageYOffset,但它总是返回0,访问保护中的ViewChild总是返回null 任何人都可以指导如何实现这一点吗?有两种方法可以帮助您 首先,从Ionic 4开始,您可以使用平台功能注册后退按钮处理程序: 其次,您可以使用

我正在尝试实现一个类似于Facebook的功能,也就是说,如果你滚动了新闻提要,按下硬件后退按钮,你就会进入列表的顶部

为此,我认为
canDeactivate
Router-Guards
将是正确的方法

但是我无法找到一种方法来检查页面是否被滚动

我尝试了
window.pageYOffset
,但它总是返回0,访问保护中的ViewChild总是返回null


任何人都可以指导如何实现这一点吗?

有两种方法可以帮助您

首先,从Ionic 4开始,您可以使用
平台
功能注册后退按钮处理程序:

其次,您可以使用Ionic 4的更多功能,称为
scrollEvents

我已在其他答案中解释了如何使用此功能:

希望这能让你朝着正确的方向前进

我认为最后一个答案应该能解决你的大部分问题,所以像这样:

古怪的乔利有一个好主意

首先,您需要
离子内容上的
滚动事件

<ion-header>
  <ion-toolbar>
    <ion-title>
      Ion Content Scroll
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content [scrollEvents]="true">
 <!-- your content in here -->
</ion-content>

你需要明确你的标签,因为不同的爱奥尼亚版本做的事情非常不同。我已经根据你的题目“ionic4”把它清理干净了。
<ion-header>
  <ion-toolbar>
    <ion-title>
      Ion Content Scroll
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content [scrollEvents]="true">
 <!-- your content in here -->
</ion-content>
import { Component, ViewChild } from '@angular/core';
import { Platform, IonContent } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
    // This property will save the callback which we can unsubscribe when we leave this view
    public unsubscribeBackEvent: any;
    @ViewChild(IonContent) content: IonContent;

    constructor(
        private platform: Platform
    ) { }

    //Called when view is loaded as ionViewDidLoad() removed from Ionic v4
    ngOnInit(){
        this.initializeBackButtonCustomHandler();
    }


    //Called when view is left
    ionViewWillLeave() {
        // Unregister the custom back button action for this page
        this.unsubscribeBackEvent && this.unsubscribeBackEvent();
    }

    initializeBackButtonCustomHandler(): void {
        this.unsubscribeBackEvent = this.platform.backButton.subscribeWithPriority(999999,  () => {
            this.content.scrollToPoint(0,0,1500);
        });
        /* here priority 101 will be greater then 100 
        if we have registerBackButtonAction in app.component.ts */
    }
}