Vue로 PWA 개발 - 그랜파 개발자
앱의 페이지 레이아웃을 만들어 봅시다.
To create an App.vue file using Vuetify with a v-navigation-drawer and v-app-bar, follow the example below. This will set up a basic layout that includes a navigation drawer for side navigation and an app bar at the top.
v-navigation-drawer 및 v-app-bar와 함께 Vuetify를 사용하여 App.vue 파일을 생성하려면 아래 예를 따르십시오. 그러면 측면 탐색을 위한 탐색 창과 상단에 앱 바가 포함된 기본 레이아웃이 설정됩니다.
If Vuetify isn't set up in your project yet, you can add it by running the command:
프로젝트에 Vuetify가 아직 설정되지 않은 경우 다음 명령을 실행하여 추가할 수 있습니다.
vue add vuetify
Follow the prompts to integrate Vuetify into your project.
프롬프트에 따라 Vuetify를 프로젝트에 통합하세요.
Create or modify the App.vue file to include a v-navigation-drawer and v-app-bar.
v-navigation-drawer 및 v-app-bar를 포함하도록 App.vue 파일을 생성하거나 수정합니다.
<template>
<v-app>
<!-- App Bar -->
<v-app-bar app>
<v-app-bar-nav-icon @click="drawer = !drawer"></v-app-bar-nav-icon>
<v-toolbar-title>My PWA</v-toolbar-title>
</v-app-bar>
<!-- Navigation Drawer -->
<v-navigation-drawer app v-model="drawer" :clipped="$vuetify.breakpoint.lgAndUp">
<v-list>
<v-list-item-group>
<!-- List Item 1 -->
<v-list-item @click="">
<v-list-item-icon>
<v-icon>mdi-home</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>Home</v-list-item-title>
</v-list-item-content>
</v-list-item>
<!-- List Item 2 -->
<v-list-item @click="">
<v-list-item-icon>
<v-icon>mdi-information</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>About</v-list-item-title>
</v-list-item-content>
</v-list-item>
<!-- List Item 3 -->
<v-list-item @click="">
<v-list-item-icon>
<v-icon>mdi-contact-mail</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>Contact</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list-item-group>
</v-list>
</v-navigation-drawer>
<!-- Main Content -->
<v-main>
<v-container fluid>
<h1>Welcome to My PWA!</h1>
<p>This is the start page of your Progressive Web App.</p>
</v-container>
</v-main>
</v-app>
</template>
<script>
export default {
data() {
return {
drawer: false, // State to toggle the navigation drawer
};
},
};
</script>
<style>
/* Add custom styles here if needed */
</style>
v-app: The outer wrapper that defines the application layout for Vuetify. This component is required for any Vuetify app.
v-app: Vuetify의 애플리케이션 레이아웃을 정의하는 외부 래퍼입니다. 이 구성 요소는 모든 Vuetify 앱에 필요합니다.
v-app-bar: This is the top app bar that stays at the top of the viewport. The app prop ensures it is correctly positioned.
v-app-bar: 뷰포트 상단에 유지되는 상단 앱 바입니다. app 소품은 올바른 위치에 있는지 확인합니다.
v-app-bar-nav-icon: This is an icon button (typically a hamburger menu) that toggles the navigation drawer. The @click event toggles the drawer state.
v-app-bar-nav-icon: 탐색 창을 전환하는 아이콘 버튼(일반적으로 햄버거 메뉴)입니다. @click 이벤트는 서랍 상태를 전환합니다.
v-toolbar-title: Displays the title text within the app bar.
v-toolbar-title: 앱 바 내에 제목 텍스트를 표시합니다.
v-navigation-drawer: A sidebar that slides in from the side of the screen. The v-model="drawer" syncs its visibility with the component's drawer data property.
v-navigation-drawer: 화면 측면에서 미끄러지는 사이드바입니다. v-model="drawer"는 가시성을 구성요소의 drawer 데이터 속성과 동기화합니다.
v-list and v-list-item: Used to create a list of navigation links in the drawer. Each v-list-item represents a navigation option.
v-list 및 v-list-item: 서랍에 탐색 링크 목록을 만드는 데 사용됩니다. 각 v-list-item은 탐색 옵션을 나타냅니다.
v-main: The main content area of the app. This is where the app's primary content is displayed.
v-main: 앱의 주요 콘텐츠 영역입니다. 여기에는 앱의 주요 콘텐츠가 표시됩니다.
v-container: A wrapper for the main content, providing padding and responsive layout capabilities.
v-container: 패딩 및 반응형 레이아웃 기능을 제공하는 기본 콘텐츠의 래퍼입니다.
The :clipped="$vuetify.breakpoint.lgAndUp" prop on the v-navigation-drawer makes the drawer clipped under the app bar on large screens and up. You can adjust this based on your design requirements.
v-navigation-drawer의 :clipped="$vuetify.breakpoint.lgAndUp" 속성은 대형 화면 이상의 앱 바 아래에서 서랍을 잘립니다. 디자인 요구 사항에 따라 이를 조정할 수 있습니다.
Customize the icons and content in the v-list-item according to your app’s needs.
앱의 필요에 따라 'v-list-item'의 아이콘과 콘텐츠를 맞춤설정하세요.
You can add more interactivity or routing by using Vue Router to navigate between different pages or components when a list item is clicked.
목록 항목을 클릭할 때 Vue Router를 사용하여 다른 페이지나 구성 요소 사이를 탐색하면 더 많은 상호 작용이나 라우팅을 추가할 수 있습니다.
Once you’ve set up App.vue with the above code, run your application to see the layout:
위 코드로 App.vue를 설정한 후 애플리케이션을 실행하여 레이아웃을 확인하세요.
npm run serve
Your application should now have a responsive navigation drawer and app bar using Vuetify, suitable for a Progressive Web App.
이제 애플리케이션에는 Progressive Web App에 적합한 Vuetify를 사용하는 반응형 탐색 창과 앱 바가 있어야 합니다.
<router-view>는 Vue Router를 사용할 때, 현재 라우트와 일치하는 컴포넌트를 표시하는 자리표시자 컴포넌트입니다. Vue 애플리케이션에서 페이지 간의 탐색을 관리할 수 있도록 도와줍니다. 기본적으로 <router-view>는 Vue Router에 의해 정의된 경로에 따라 해당 경로와 연결된 컴포넌트를 렌더링합니다.
아래는 이전에 제공한 App.vue 예시에 <router-view>를 추가하는 방법입니다. 이 구성 요소를 추가하면 각 페이지(또는 경로)가 v-main 영역에 표시됩니다.
<template>
<v-app>
<!-- App Bar -->
<v-app-bar app>
<v-app-bar-nav-icon @click="drawer = !drawer"></v-app-bar-nav-icon>
<v-toolbar-title>My PWA</v-toolbar-title>
</v-app-bar>
<!-- Navigation Drawer -->
<v-navigation-drawer app v-model="drawer" :clipped="$vuetify.breakpoint.lgAndUp">
<v-list>
<v-list-item-group>
<!-- List Item 1 -->
<v-list-item @click="$router.push('/')">
<v-list-item-icon>
<v-icon>mdi-home</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>Home</v-list-item-title>
</v-list-item-content>
</v-list-item>
<!-- List Item 2 -->
<v-list-item @click="$router.push('/about')">
<v-list-item-icon>
<v-icon>mdi-information</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>About</v-list-item-title>
</v-list-item-content>
</v-list-item>
<!-- List Item 3 -->
<v-list-item @click="$router.push('/contact')">
<v-list-item-icon>
<v-icon>mdi-contact-mail</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>Contact</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list-item-group>
</v-list>
</v-navigation-drawer>
<!-- Main Content with Router View -->
<v-main>
<v-container fluid>
<!-- Router View to display route components -->
<router-view></router-view>
</v-container>
</v-main>
</v-app>
</template>
<script>
export default {
data() {
return {
drawer: false, // State to toggle the navigation drawer
};
},
};
</script>
<style>
/* Add custom styles here if needed */
</style>
Vue Router가 아직 설치되어 있지 않다면, 설치하고 기본 라우터 설정을 추가해야 합니다.
npm install vue-router
import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';
import About from '../views/About.vue';
import Contact from '../views/Contact.vue';
Vue.use(VueRouter);
const routes = [
{
path: '/',
name: 'Home',
component: Home,
},
{
path: '/about',
name: 'About',
component: About,
},
{
path: '/contact',
name: 'Contact',
component: Contact,
},
];
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes,
});
export default router;
import Vue from 'vue';
import App from './App.vue';
import router from './router';
import vuetify from './plugins/vuetify'; // Vuetify 설정이 여기에 있어야 합니다.
Vue.config.productionTip = false;
new Vue({
router,
vuetify,
render: (h) => h(App),
}).$mount('#app');
이제 애플리케이션이 특정 경로로 탐색할 때마다 App.vue의 <router-view> 영역에서 해당 컴포넌트가 렌더링됩니다. <v-navigation-drawer>의 링크를 클릭하면 적절한 경로로 이동하며, 이에 따라 <router-view>에 해당 컴포넌트가 표시됩니다.
To dynamically create navigation items in a v-navigation-drawer using an array, you can define an array of routes in your Vue component's data and then loop through this array to generate the items in the drawer.
배열을 사용하여 'v-navigation-drawer'에 탐색 항목을 동적으로 생성하려면 Vue 구성 요소의 데이터에 경로 배열을 정의한 다음 이 배열을 반복하여 서랍에 항목을 생성할 수 있습니다.
Here’s how you can modify the App.vue to use an array to define each router item in the v-navigation-drawer
.
배열을 사용하여 v-navigation-drawer의 각 라우터 항목을 정의하도록 App.vue를 수정하는 방법은 다음과 같습니다.
Define the Routes Array: Create an array in your component's data that contains the information for each route you want to display in the navigation drawer.
경로 배열 정의: 탐색 창에 표시하려는 각 경로에 대한 정보가 포함된 구성 요소 데이터에 배열을 만듭니다.
Loop Through the Routes Array in the Template: Use Vue's v-for directive to loop through this array and dynamically create v-list-item components for each route.
템플릿의 경로 배열을 통해 반복: Vue의 v-for 지시문을 사용하여 이 배열을 반복하고 각 경로에 대한 v-list-item 구성 요소를 동적으로 생성합니다.
Here's how you can modify the App.vue:
'App.vue'를 수정하는 방법은 다음과 같습니다.
<template>
<v-app>
<!-- App Bar -->
<v-app-bar app>
<v-app-bar-nav-icon @click="drawer = !drawer"></v-app-bar-nav-icon>
<v-toolbar-title>My PWA</v-toolbar-title>
</v-app-bar>
<!-- Navigation Drawer -->
<v-navigation-drawer app v-model="drawer" :clipped="$vuetify.breakpoint.lgAndUp">
<v-list>
<v-list-item-group>
<!-- Loop through routes array to create navigation items -->
<v-list-item
v-for="(route, index) in routes"
:key="index"
@click="$router.push(route.path)"
>
<v-list-item-icon>
<v-icon>{{ route.icon }}</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>{{ route.title }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list-item-group>
</v-list>
</v-navigation-drawer>
<!-- Main Content with Router View -->
<v-main>
<v-container fluid>
<!-- Router View to display route components -->
<router-view></router-view>
</v-container>
</v-main>
</v-app>
</template>
<script>
export default {
data() {
return {
drawer: false, // State to toggle the navigation drawer
// Array of routes for navigation items
routes: [
{ title: 'Home', path: '/', icon: 'mdi-home' },
{ title: 'About', path: '/about', icon: 'mdi-information' },
{ title: 'Contact', path: '/contact', icon: 'mdi-contact-mail' },
// Add more routes here as needed
],
};
},
};
</script>
<style>
/* Add custom styles here if needed */
</style>
This array contains objects for each navigation item.
이 배열에는 각 탐색 항목에 대한 개체가 포함되어 있습니다.
Each object has a title, path, and icon property.
각 객체에는 '제목', '경로', '아이콘' 속성이 있습니다.
title: Display name of the route in the navigation drawer.
제목: 탐색 창에 표시되는 경로 이름입니다.
path: The router path that this item will navigate to when clicked.
경로: 이 항목을 클릭하면 탐색할 라우터 경로입니다.
icon: The Vuetify icon name for the item.
icon: 항목의 Vuetify 아이콘 이름입니다.
Ensure your Vue Router setup matches the paths defined in the routes array. If you haven't already set up Vue Router, follow the instructions in the previous message to configure it.
Vue 라우터 설정이 routes 배열에 정의된 경로와 일치하는지 확인하세요. Vue Router를 아직 설정하지 않은 경우 이전 메시지의 지침에 따라 구성하세요.
After setting up the dynamic navigation drawer, run your application with:
동적 탐색 창을 설정한 후 다음을 사용하여 애플리케이션을 실행하세요.
npm run serve
This setup allows you to manage your navigation items easily by updating the routes array, making it simpler to add, remove, or modify navigation items without altering the template structure significantly.
이 설정을 사용하면 '경로' 배열을 업데이트하여 탐색 항목을 쉽게 관리할 수 있으므로 템플릿 구조를 크게 변경하지 않고도 탐색 항목을 더 간단하게 추가, 제거 또는 수정할 수 있습니다.