Skip to content

Vue Router

Jin Young Park edited this page Dec 21, 2020 · 1 revision

vue Router์™€ keep-alive ๋ฅผ ์ด์šฉํ•œ ๋™์  ์ปดํฌ๋„ŒํŠธ ์บ์‹ฑ

์ดˆ๊ธฐ ๋ฌธ์ œ ํ•ด๊ฒฐ

ํ™”๋ฉด๊ณผ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ id์— ๋”ฐ๋ฅธ project์ •๋ณด๋ฅผ ๋ Œ๋”๋งํ•ด์ฃผ๊ธฐ ์œ„ํ•ด projectId๋ฅผ parameter๋กœ ๊ฐ–๋Š” ๋ผ์šฐํ„ฐ๋ฅผ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ex) project/1, project/2

image

๊ทธ๋Ÿฌ๋‚˜ vue Router์—์„œ๋Š” ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ฐ€๋“œ์— ์˜ํ•ด params ๋ณ€๊ฒฝ์— ๋”ฐ๋ฅธ ๋ฆฌ๋ Œ๋”๋ง์ด ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

image

์ด์— ๋ผ์šฐํ„ฐ์˜ beforeRouteUpdate ๋ผ์ดํ”„์‚ฌ์ดํด์„ ํ™œ์šฉํ•ด parameter ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  data๋ฅผ fetchํ•˜๋Š” ๋กœ์ง์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

beforeRouteUpdate(to, from, next) {
    this.fetchCurrentProject(to.params.projectId);
next();
  },

๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๊ธฐ์„œ์˜ ๋ฌธ์ œ์ ์€ ํ•œ๋ฒˆ ๋ฐฉ๋ฌธํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ๋ฐฉ๋ฌธํ•  ๊ฒฝ์šฐ์—๋„ data fetch๋ฅผ ๋งค๋ฒˆ ํ•ด์˜จ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ปดํฌ๋„ŒํŠธ ์บ์‹ฑ

๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ router์— key๊ฐ’์„ ๋ถ€์—ฌํ•˜๋ฉด parameter์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ„์ฃผ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ , keep-alive๋ผ๋Š” ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ์‹ธ๋ฉด ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์บ์‹ฑ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

<keep-alive>
  <router-view key="$route.params.projectId"></router-view>
</keep-alive>

keep-alive๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ๊ฐ€ destroy๋˜์ง€ ์•Š๊ณ  active์—์„œ inactive ์ƒํƒœ๋กœ ์ „ํ™˜๋˜์–ด ์žฌ๋ฐฉ๋ฌธ์‹œ์—๋„ ๋‹ค์‹œ created๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ์บ์‹ฑ๋ฉ๋‹ˆ๋‹ค.

image

์ด๋ฅผ ํ†ตํ•ด ์ตœ์ดˆ route ๋ฐฉ๋ฌธ์‹œ์—๋งŒ data fetch๋ฅผ ํ•ด์˜ค๊ณ  ์ปดํฌ๋„ŒํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์บ์‹ฑํ•˜์—ฌ ๋ฆฌ๋ Œ๋”๋ง์„ ๋ฐฉ์ง€ํ•˜๊ณ  API ํ˜ธ์ถœ ํšŸ์ˆ˜๋ฅผ ๊ฐ์ถ•ํ–ˆ์Šต๋‹ˆ๋‹ค.

Clone this wiki locally