์ด์ ํ์ฌ ๋ก๊ทธ์ธ ์ค์ธ ์ ์ ์
์ ๋ณด๋ฅผ ์ฌ์ฉํ์ฌ
ํ๋กํ์นด๋ ๋ฐ ์ ์ ์ ๋ณด๊ฐ ํ์ํ ๊ณณ์ ์ฌ์ฉํด ๋ณด๋๋ก ํ๋น
https://velog.io/@jcs526/Vue3-computed-%EC%82%AC%EC%9A%A9%EB%B0%A9%EB%B2%95
[Vue3] computed ์ฌ์ฉ๋ฐฉ๋ฒ
computed ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ณ์์ ํ ๋นํด์ฃผ๋ฉด ๊ณ์ฐ๋ ๊ฐ์ด ์ง์ ๋๋ค.
velog.io
import { ref, onBeforeMount, computed } from "vue";
const currentUser = computed(() => store.state.user);
return { routes, showProfileDropdown, onLogout, currentUser };
currentUser์ ์ ์ฅ๋์ด์๋ email๊ณผ username์ ํ์ํ๋ค.
<div class="mx-4">
<div class="font-bold text-sm">{{ currentUser.email }}</div>
<div class="text-left text-sm text-gray-500">
@{{ currentUser.username }}
</div>
</div>
์ด๋ฏธ์ง๋ ๋์ผํ๊ฒ ์ฌ์ฉํด๋ณธ
<img
:src="currentUser.profile_image_url"
class="w-10 h-10 rounded-full"
/>
๊ทธ๋ฆฌ๊ณ ํ์ฌ๋
์ฌ์ด๋ ๋ฉ๋ด๋ฅผ ํด๋ฆญํด์ ๋ฉ๋ด๋ฅผ ์ด๋ํด๋ ํ์ฌ ์ด๋ ๋ฉ๋ด์ ์๋์ง ํ์๋์ง ์๋๋ค
๊ทธ๋์ ํ์ฌ ์๋ ๋ฉ๋ด์ ํฐํธ์ ์์์ ์
ํ์ค๊ฑฐ์
router -> index.js ์ routes์ name์ ์ง์ ํ๋ค.
const routes = [
{
path: "/",
name: "home",
component: Home,
title: "ํ",
icon: "fas fa-home fa-fw text-2xl",
meta: { isMenu: true, layout: "DefaultLayout", requireAuth: true },
},
{
path: "/",
name: "explore",
component: Home,
title: "ํ์ํ๊ธฐ",
icon: "fas fa-hashtag fa-fw text-2xl",
meta: { isMenu: true, layout: "DefaultLayout", requireAuth: true },
},
{
path: "/notifications",
name: "notifications",
component: Notifications,
title: "์๋ฆผ",
icon: "far fa-bell fa-fw text-2xl",
meta: { isMenu: true, layout: "DefaultLayout", requireAuth: true },
},
{
path: "/messages",
name: "messages",
component: Messages,
title: "์ชฝ์ง",
icon: "far fa-envelope fa-fw text-2xl",
meta: { isMenu: true, layout: "DefaultLayout", requireAuth: true },
},
{
path: "/",
name: "bookmarks",
component: Home,
title: "๋ถ๋งํฌ",
icon: "far fa-bookmark fa-fw text-2xl",
meta: { isMenu: true, layout: "DefaultLayout", requireAuth: true },
},
{
path: "/",
name: "list",
component: Home,
title: "๋ฆฌ์คํธ",
icon: "far fa-list-alt fa-fw text-2xl",
meta: { isMenu: true, layout: "DefaultLayout", requireAuth: true },
},
{
path: "/profile",
name: "profile",
component: Profile,
title: "ํ๋กํ",
icon: "far fa-user fa-fw text-2xl",
meta: { isMenu: true, layout: "DefaultLayout", requireAuth: true },
},
{
path: "/",
name: "more",
component: Home,
title: "๋๋ณด๊ธฐ",
icon: "fas fa-ellipsis-h fa-fw text-2xl",
meta: { isMenu: true, layout: "DefaultLayout", requireAuth: true },
},
{
path: "/register",
name: "register",
component: Register,
meta: { isMenu: false, layout: "EmptyLayout" },
},
{
path: "/login",
name: "login",
component: Login,
meta: { isMenu: false, layout: "EmptyLayout" },
},
];
๋ผ์ฐํฐ๋ฅผ ๋ฆฌํดํ
return { routes, showProfileDropdown, onLogout, currentUser, router };
์ฌ์ด๋ ๋ฉ๋ด class์
ํ์ฌ ๋ผ์ฐํฐ์ name๊ณผ route์ name์ด ๋์ผํ๋ฉด text-primary css๋ฅผ ์ฃผ๊ณ ์๋๋ฉด ์์ค
<!-- ํธ์ํฐ ์ฌ์ด๋ ๋ฉ๋ด -->
<div class="flex flex-col items-start space-y-1">
<router-link
:to="route.path"
:class="`hover:text-primary hover:bg-blue-50 px-4 py-2 rounded-full cursor-pointer ${
router.currentRoute.value.name == route.name ? 'text-primary' : ''
}`"
v-for="route in routes"
:key="route"
>
<div v-if="route.meta.isMenu">
<i :class="route.icon"></i>
<span class="ml-3 text-xl hidden xl:inline-block">{{
route.title
}}</span>
</div>
</router-link>
</div>