From 7d6b08d2253add01bcee9917b0f5a659fae931d4 Mon Sep 17 00:00:00 2001 From: fxyyf1998 Date: Tue, 15 Jul 2025 09:45:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9EFAQ-Web=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=A4=B1=E8=B4=A5=E6=97=B6=E7=9A=84=E7=99=BD=E5=B1=8F?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E5=A6=82=E4=BD=95=E6=94=B9=E4=B8=BA=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E9=94=99=E8=AF=AF=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/pages/CustomFailurePage.ets | 45 +++++++++++++++++++ .../rawfile/custom_failure_page.html | 0 2 files changed, 45 insertions(+) create mode 100644 ArkWebKit/entry/src/main/ets/pages/CustomFailurePage.ets create mode 100644 ArkWebKit/entry/src/main/resources/rawfile/custom_failure_page.html diff --git a/ArkWebKit/entry/src/main/ets/pages/CustomFailurePage.ets b/ArkWebKit/entry/src/main/ets/pages/CustomFailurePage.ets new file mode 100644 index 0000000..f4632c4 --- /dev/null +++ b/ArkWebKit/entry/src/main/ets/pages/CustomFailurePage.ets @@ -0,0 +1,45 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:Web加载失败时默认的白屏页面如何改为自定义的错误页 +*/ + +// [Start CustomFailurePage] +import { webview } from '@kit.ArkWeb'; +import { BusinessError } from '@kit.BasicServicesKit'; + +@Entry +@Component +struct Index { + controller: webview.WebviewController = new webview.WebviewController(); + + build() { + Stack() { + Web({ src: 'www.example.com', controller: this.controller }) + .onErrorReceive((event) => { + if (event && event.request.isMainFrame()) { + try { + // 加载自定义错误页面 + this.controller.loadUrl($rawfile('custom_failure_page.html')); + } catch (error) { + console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); + } + } + }) + } + } +} +// [End CustomFailurePage] \ No newline at end of file diff --git a/ArkWebKit/entry/src/main/resources/rawfile/custom_failure_page.html b/ArkWebKit/entry/src/main/resources/rawfile/custom_failure_page.html new file mode 100644 index 0000000..e69de29 -- Gitee