在Vue 3、Angular 14或React中创建离线屏幕的方法
在本教程中,我们将学习如何使用网络API在Vue 3、Angular 14和React应用程序中创建离线屏幕。网络API提供网络和连接信息,使我们能够处理离线场景并提供更好的用户体验。为了更深入地检查可达性超出简单的在线/离线标志 @capgo/capacitor-network-diagnostics 可以从本机code测试URL可达性、TCP端口和WebSocket握手
前提条件
开始之前,请确保你已经安装以下内容:
- 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__
Installing the Network API
包,它提供了网络 __CAPGO_KEEP_0__。 @capacitor/network package, which provides the Network API.
framework_react
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)中添加一个 <div> 元素,id 为 offline-screen 以显示离线屏幕消息:
<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元素,id为 <div> 来显示离线屏幕消息: 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 -->
Now, when the user goes offline, the offline screen will be displayed. When the user comes back online, the offline screen will be hidden. app.component.css Angular 14
#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;
}
支持的方法和接口
网络__CAPGO_KEEP_0__提供了几个方法和接口来帮助您处理网络连接。以下是其中一些关键的:
The Network API provides several methods and interfaces to help you handle the network connection. Here are some of the key ones:
getStatus():监听网络连接的变化。addListener('networkStatusChange', ...)继续使用__CAPGO_KEEP_0__在Vue、Angular、React中创建脱机屏幕
Keep going from Offline Screen in Vue, Angular, React with Capacitor
如果您正在使用 在 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的原生能力。