Angular:在运行打印之前获取数据,我缺少什么?

发布于 2025-02-13 22:18:41 字数 1880 浏览 1 评论 0原文

我正在研究一个Angular网页,该网页对API运行了多个提取请求。 某些结果显示在子组件中,这些结果是用户可能打开的封闭面板。

当前,当页面本身加载时,所有获取请求都会运行,但是我将它们更改为仅在必要时运行。当用户打开给定面板时,只有这样获取相应的数据。

对于这些面板,打开时触发提取请求是简单的部分。 当我想打印页面时,困难的部分是。

打印视图包括所有数据,包括面板中的数据(使用CSS @Media Print隐藏面板标头和边框)。

为了获取所有相关的数据,必须在打印之前执行所有获取请求。

ergo,我们有两种情况:

  • 在打开关联的面板
  • 获取所有 data 打印之前,

我正在努力打印案例。

我正在尝试将hostlistener和window.beforeprint事件使用。 获取数据是类似的:

//Ideal place for @HostListener('window:beforeprint', [])
fetchDataIfNotYetExecuted() {  // Executed when opening panel
    if (!this.dataHasBeenLoaded) {
        console.log('Loading data');
        this.fetchData();
    }
}

fetchData() {
    this.myApi.fetchData().subscribe({
        next: response => {
            this.data = response.data;
            this.dataHasBeenLoaded = true;
            console.log('Data load complete');
        },
        error => {
            console.log(error);
        }
    })
}

myapi是运行httpclient的服务。

当前, 强>打印预览打开,但是,我在这里拥有的是异步代码。

我尝试使用异步/等待:

@HostListener('window:beforeprint', [])
async fetchDataIfNotYetExecuted() {  
    if (!this.dataHasBeenLoaded) {
        console.log('Loading data');
        await this.fetchData();
    }
}

async fetchData() {
    try {
        const response = await firstValueFrom(this.myApi.fetchData());
        this.data = response.data;
        this.dataHasBeenLoaded = true;
        console.log('Data load complete');
    } catch(error) {
        console.log(error);
    }
}

因为异步/等待承诺,而不是可观察的,我必须使用rxjs.s.firstvaluefrom将可观察到的诺言转换为诺言。

不幸的是,我无法使其正常工作。

每当我点击网页的打印按钮或运行Ctrl + P时,我都会获得一个Print Preview 没有数据,而我在控制台窗口中同时看到Fetch方法正在运行(因此,如果我再次点击“打印”,而无需重新加载页面,数据就在那里)。

我需要在进行打印预览之前进行

我想念什么?

I'm working on an Angular webpage which runs several fetch requests towards an API.
Some of the results are displayed in sub-components, which are closed panels that may be opened by the user.

Currently, all fetch requests run when the page itself is loaded, but I'm changing them to run only when necessary. When a user opens a given panel, only then should the corresponding data be fetched.

For these panels, triggering the fetch requests when opened is the easy part.
The difficult part comes when I wish to print the page.

The print view includes all data, including the ones in the panels (while the panel header and borders are hidden, using CSS @media print).

In order to get all the associated data, all fetch requests must have been executed before printing.

Ergo, we have two cases:

  • Fetch data when opening the associated panel
  • Fetch all data before printing

I'm struggling with the print case.

I'm trying to use HostListener, with the window.beforePrint event.
Fetching data is currently built like this:

//Ideal place for @HostListener('window:beforeprint', [])
fetchDataIfNotYetExecuted() {  // Executed when opening panel
    if (!this.dataHasBeenLoaded) {
        console.log('Loading data');
        this.fetchData();
    }
}

fetchData() {
    this.myApi.fetchData().subscribe({
        next: response => {
            this.data = response.data;
            this.dataHasBeenLoaded = true;
            console.log('Data load complete');
        },
        error => {
            console.log(error);
        }
    })
}

MyApi is a service that runs HttpClient.get, which leads to use of Observables, Subscriptions with data-prosessing, storing, etc, etc.

I need all this to be done before the print preview opens, and yet, what I have here is asynchronous code.,

I have made an attempt with async/await:

@HostListener('window:beforeprint', [])
async fetchDataIfNotYetExecuted() {  
    if (!this.dataHasBeenLoaded) {
        console.log('Loading data');
        await this.fetchData();
    }
}

async fetchData() {
    try {
        const response = await firstValueFrom(this.myApi.fetchData());
        this.data = response.data;
        this.dataHasBeenLoaded = true;
        console.log('Data load complete');
    } catch(error) {
        console.log(error);
    }
}

Since async/await works with Promises, and not Observables, I have to convert the Observable into a Promise, using RxJs.firstValueFrom.

Unfortunately, I can't get this to work properly.

Whenever I hit the webpage's print button, or run CTRL + P, I get a print preview without the data, while I see simultaneously in the console window that the fetch method is running, (so, if I hit 'print' again without reloading the page, the data is there).

I need the fetch to be done before proceeding to the print preview.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文