如何在 Vue 3、Angular 14 或 React 中创建离线屏幕
在本教程中,我们将学习如何使用网络API在 Vue 3、Angular 14 和 React 应用程序中创建离线屏幕。网络API提供网络和连接信息,使我们能够处理离线场景并提供更好的用户体验。为了更深入地检查可达性超出简单的在线/离线标志 @capgo/capacitor-network-diagnostics 可以测试 URL 可达性、TCP 端口和 WebSocket 握手从本机code.
前提条件
在开始之前,请确保您已安装以下内容:
- Node.js (版本 14 或更高)
- Vue CLI
- Angular CLI
- Create React App
设置项目
首先,让我们使用各个框架的构建工具创建一个新项目。
Vue 3
打开您的终端并运行以下命令以创建一个新的 Vue 3 项目:
vue create offline-screen-vue3
选择默认预设并等待项目创建完成。
Angular 14
打开终端并运行以下命令创建一个新的 Angular 14 项目:
ng new offline-screen-angular14
按照提示操作,到询问额外功能时,选择“路由”并按空格键。 等待项目创建完成。 React
打开终端并运行以下命令创建一个新的 React 项目:
等待项目创建完成。
npx create-react-app offline-screen-react
安装网络 __CAPGO_KEEP_0__
现在,让我们安装提供网络 API 的包。
打开终端并切换到项目目录,然后运行以下命令安装包: @capacitor/network API
__CAPGO_KEEP_0__
npm install @capacitor/network
For Capacitor 项目中,运行以下命令同步本地项目文件:
npx cap sync
确保您已在全局安装 Capacitor CLI
npm install -g @capacitor/cli
离线屏幕
下一步,我们将在每个框架中实现离线屏幕功能。我们将在用户脱离网络时显示一个简单的消息。
Vue 3
在您的 Vue 3 项目中,打开 src/main.js 文件并导入 Network 模块从 @capacitor/network:
import { createApp } from 'vue';
import { Network } from '@capacitor/network';
const app = createApp(App);
// Your application setup
app.mount('#app');
Network.addListener('networkStatusChange', status => {
if (status.connected) {
// User is back online
// Hide the offline screen
document.getElementById('offline-screen').style.display = 'none';
} else {
// User is offline
// Display the offline screen
document.getElementById('offline-screen').style.display = 'block';
}
});
const logCurrentNetworkStatus = async () => {
const status = await Network.getStatus();
console.log('Network status:', status);
};
在您的应用程序模板(),添加一个 App.vue元素,id 为 <div> 以显示离线屏幕消息: offline-screen Make sure you have the __CAPGO_KEEP_0__ __CAPGO_KEEP_1__ installed globally by running:
<template>
<div id="app">
<div id="offline-screen">
<h1>You are offline</h1>
<p>Please check your internet connection and try again.</p>
</div>
<!-- Your application content -->
</div>
</template>
<style>
#offline-screen {
display: none;
text-align: center;
padding: 20px;
background-color: #f2f2f2;
}
#offline-screen h1 {
font-size: 24px;
}
#offline-screen p {
font-size: 16px;
}
</style>
Now, when the user goes offline, the offline screen will be displayed. When the user comes back online, the offline screen will be hidden.
Angular 14
在您的Angular 14项目中,打开文件并导入模块 src/app/app.component.ts 模块 Network 来自 @capacitor/network:
import { Component } from '@angular/core';
import { Network } from '@capacitor/network';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor() {
Network.addListener('networkStatusChange', status => {
if (status.connected) {
// User is back online
// Hide the offline screen
document.getElementById('offline-screen').style.display = 'none';
} else {
// User is offline
// Display the offline screen
document.getElementById('offline-screen').style.display = 'block';
}
});
}
logCurrentNetworkStatus = async () => {
const status = await Network.getStatus();
console.log('Network status:', status);
};
}
在您的应用程序模板(app.component.html)中,添加一个 <div> 元素,id为 offline-screen ,以显示离线屏幕消息:
<div id="offline-screen">
<h1>You are offline</h1>
<p>Please check your internet connection and try again.</p>
</div>
<!-- Your application content -->
将以下样式添加到文件中: app.component.css Now, when the user goes offline, the offline screen will be displayed. When the user comes back online, the offline screen will be hidden.
#offline-screen {
display: none;
text-align: center;
padding: 20px;
background-color: #f2f2f2;
}
#offline-screen h1 {
font-size: 24px;
}
#offline-screen p {
font-size: 16px;
}
Angular 14
React
在您的 React 项目中,打开文件并导入模块 src/App.js 文件 Network 模块 @capacitor/network:
import React, { useEffect } from 'react'
import { Network } from '@capacitor/network'
function App() {
useEffect(() => {
Network.addListener('networkStatusChange', (status) => {
if (status.connected) {
// User is back online
// Hide the offline screen
document.getElementById('offline-screen').style.display = 'none'
}
else {
// User is offline
// Display the offline screen
document.getElementById('offline-screen').style.display = 'block'
}
})
}, [])
const logCurrentNetworkStatus = async () => {
const status = await Network.getStatus()
console.log('Network status:', status)
}
return (
<div id="app">
<div id='offline-screen'>
<h1>You are offline</h1>
<p>Please check your internet connection and try again.</p>
</div>
{/* Your application content */}
</div>
)
}
export default App
从 App.css 添加以下样式到文件
#offline-screen {
display: none;
text-align: center;
padding: 20px;
background-color: #f2f2f2;
}
#offline-screen h1 {
font-size: 24px;
}
#offline-screen p {
font-size: 16px;
}
文件
现在,当用户脱离网络时,会显示离线屏幕。当用户重新上线时,离线屏幕会被隐藏。
The Network API provides several methods and interfaces to help you handle the network connection. Here are some of the key ones:
getStatus()网络__CAPGO_KEEP_0__提供了几个方法和接口来帮助您处理网络连接。以下是其中一些关键的:addListener('networkStatusChange', ...):查询当前网络连接的状态
Keep going from Offline Screen in Vue, Angular, React with Capacitor
If you are using 在 Vue、Angular、React 中使用 Capacitor 来规划原生媒体和界面行为,连接它与 使用 @capgo/capacitor-live-activities 为原生能力在使用 @capgo/capacitor-live-activities 使用 @capgo/capacitor-live-activities 为实现细节在 @capgo/capacitor-live-activities 使用 @capgo/capacitor-video-player 为原生能力在使用 @capgo/capacitor-video-player 使用 @capgo/capacitor-video-player 为实现细节在 @capgo/capacitor-video-player, 使用 @capgo/capacitor-native-navigation 为使用@capgo/capacitor-native-navigation的原生能力。