How to Create an Offline Screen in Vue 3, Angular 14, or React
In this tutorial, we will learn how to create an offline screen in Vue 3, Angular 14, and React applications using the Network API. The Network API provides network and connectivity information, allowing us to handle offline scenarios and provide a better user experience. For deeper reachability checks beyond a simple online/offline flag, @capgo/capacitor-network-diagnostics can test URL reachability, TCP ports, and WebSocket handshakes from native code.
Prerequisites
Before we begin, make sure you have the following installed:
- Node.js (version 14 or higher)
- Vue CLI
- Angular CLI
- Create React App
Setting Up the Project
첫 번째로, 각 프레임워크에 해당하는 scaffold tool을 사용하여 새로운 프로젝트를 생성하세요.
Vue 3
터미널을 열고 다음 명령어를 실행하여 새로운 Vue 3 프로젝트를 생성하세요:
vue create offline-screen-vue3
기본 preset을 선택하고 프로젝트가 생성될 때까지 기다리세요.
Angular 14
터미널을 열고 다음 명령어를 실행하여 새로운 Angular 14 프로젝트를 생성하세요:
ng new offline-screen-angular14
안내를 따라 주시고 추가 기능에 대해 묻히면 "Routing"을 스페이스바 키를 눌러 선택하세요. 프로젝트가 생성될 때까지 기다리세요. React
터미널을 열고 다음 명령어를 실행하여 새로운 React 프로젝트를 생성하세요:
프로젝트가 생성될 때까지 기다리세요.
npx create-react-app offline-screen-react
__CAPGO_KEEP_0__
API 설치
현재, 다음을 설치해 보세요. @capacitor/network API 패키지를 설치하는 패키지입니다.
터미널을 열고 프로젝트 폴더로 이동한 후, 다음 명령어를 실행하여 패키지를 설치하세요.
npm install @capacitor/network
Capacitor 프로젝트의 경우, 다음 명령어를 실행하여 네이티브 프로젝트 파일을 동기화하세요.
npx cap sync
Capacitor와 CLI이 전역으로 설치되어 있는지 확인하세요.
npm install -g @capacitor/cli
오프라인 화면 구현
다음으로, 각 프레임워크에서 오프라인 화면 기능을 구현할 것입니다. 사용자가 오프라인 상태가 되었을 때, 간단한 메시지를 표시할 것입니다.
Vue 3
Vue 3 프로젝트에서, 다음 파일을 열고, 다음 모듈을 import하세요. src/main.js __CAPGO_KEEP_1__ 모듈을 import하세요. Network __CAPGO_KEEP_0__ @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>
사용자가 오프라인 상태가 되면 오프라인 화면이 표시되고, 사용자가 온라인 상태가 되면 오프라인 화면이 숨겨집니다.
Angular 14
Angular 14 프로젝트에서 src/app/app.component.ts 파일을 열고 Network 모듈을 import하세요. @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 To 오프라인 화면 메시지를 표시하십시오:
<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 파일:
#offline-screen {
display: none;
text-align: center;
padding: 20px;
background-color: #f2f2f2;
}
#offline-screen h1 {
font-size: 24px;
}
#offline-screen p {
font-size: 16px;
}
사용자가 오프라인이 되면 오프라인 화면이 표시되고, 사용자가 온라인이 되면 오프라인 화면이 숨겨집니다.
React
React 프로젝트에서 파일을 열고 src/App.js 모듈을 import하십시오. 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', ...): 네트워크 연결 상태의 변경을 감지합니다.
Vue, Angular, React에서 Offline Screen을 계속 진행하는 방법은 Capacitor입니다.
__CAPGO_KEEP_0__을 사용하는 경우 Vue, Angular, React에서 Offline Screen을 계속 진행하는 방법은 Capacitor입니다. native 미디어 및 인터페이스 동작을 계획하고자 할 때, __CAPGO_KEEP_0__을 연결하세요. @capgo/capacitor-live-activities를 사용하여 native 기능을 구현합니다. @capgo/capacitor-live-activities를 사용하여 native 기능을 구현합니다. @capgo/capacitor-live-activities를 사용하여 native 기능을 구현합니다. @capgo/capacitor-video-player를 사용하여 native 미디어를 구현합니다. @capgo/capacitor-video-player를 사용하여 native 미디어를 구현합니다. capgo/capacitor-video-player를 사용하는 원시 기능에 대해 capgo/capacitor-video-player capgo/capacitor-video-player의 구현 세부 사항에 대해 capgo/capacitor-native-navigation을 사용하는 capgo/capacitor-native-navigation의 원시 기능에 대해