๐์ต๊ทผ ๋ฐ์ํ ๋ฌธ์ ์ $ref undefined
mounted()์์ $ref์ ์ ๊ทผ์ ํด์ ๋ฌด์ธ๊ฐ๋ฅผ ์ ์ฉ์ ํ๋ ค๊ณ ํด ๋ดค๋๋ฐ,
$ref์ ์ ๊ทผ์ด ์๋๊ณ undefined๊ฐ ๋จ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
๋ฌด์์ด ๋ฌธ์ ์ธ์ง ์ฐพ์๋ณธ ๊ฒฐ๊ณผ
<client-only>์์ ์๋ $ref์ ์ ๊ทผ์ ํ ๋,
์ด๋ฐ์ undefined๊ฐ ๋ฐ์ํ ์ ์๋ค๋ ์ ์ ์ฐพ์๋๋ค.
์ด๋ฌํ ์ํฉ์ ์ ๋ฐ์ํ๋๊ฑธ๊น?
๋จผ์ ์ด ๋ฌธ์ ๋ฅผ ์๊ธฐ ์ํด์๋ <client-only>๋ฅผ ์ ์ฌ์ฉํ๋์ง๋ฅผ ์์์ผ ํ๊ณ ,
SSR๊ณผ CSR์ ์ฐจ์ด๋ฅผ ๋จผ์ ์ดํดํด์ผ ๋์ด์
์ค๋์ ์ด ๋ถ๋ถ์ ์ ๋ฆฌํด๋ณด๊ธฐ๋ก ํ๋ค!
โ๏ธSSR์ด๋?
Server Side Rendering์ด๋ผ๊ณ ํด์
Server์์ ๋ ๋๋ง์ด ๋ HTML ํ๋ฉด์ ๋ง๋ค์ด์ ๋ณด๋ด์ฃผ๋ ๋ฐฉ์์ ๋งํ๋ค.
SSR๋ก ์ฒ๋ฆฌ๊ฐ ๋๋ค๊ณ ํ๋ฉด ๋ชจ๋ ๋ฆฌ์์ค๋ฅผ ์๋ฒ์์ ๋๋๋ง ํ ๋ค์์
ํด๋ผ์ด์ธํธ ์ชฝ์ Return์ ํด์ฃผ๊ธฐ ๋๋ฌธ์ ํ์ด์ง๋ฅผ ๋ชจ๋ ๊ตฌ์ฑํ๋๋ฐ ์๋ฒ ์ธก์ ๋ถ๋ด์ด ์์ผ๋
๋ฐ๊ธฐ๋ง ํ๋ค๋ฉด ๋ฐ๋ก ์์ฑ๋ ํ๋ฉด์ ๋์ธ ์ ์์ด ์ด๊ธฐ ๋ก๋ฉ ์๋๊ฐ ๋น ๋ฅด๋ค.
๐งพCSR์ด๋?
SSR๊ณผ ๋ฐ๋๋๋ ๊ฒฝ์ฐ๊ฐ CSR(Client Side Rendering)์ด ์๋ค.
Client Side Rendering์ด๋ผ๊ณ ํด์
์๋ฒ์์ HTML๊ณผ Javascipt๋ฅผ ํจ๊ป ๋ณด๋ด์
ํด๋ผ์ด์ธํธ์์ ๋ ํ์ผ์ ๋ฐ์ ๊ฐ์ด ๋๋๋ง์ ํ๋ ๊ฒ์ ๋งํ๋ค.
CSR์ ๊ฒฝ์ฐ ์ผ๋จ ํ์ด์ง๊ฐ ๋จ๋, ์ฒ์ ๋น ํ์ด์ง๊ฐ ๋จ๊ณ ,
๊ทธ๋ค์์ ํ๋ฉด์ ๊ทธ๋ฆด ์๋ฐ์คํฌ๋ฆฝํธ์ HTML ์ฝ์ด Rendering ์คํํ๊ธฐ ๋๋ฌธ์
SSR๋ณด๋ค ์ด๊ธฐ ๋ก๋ฉ ์๊ฐ์ ๋ ์ค๋ ๊ฑธ๋ฆฐ๋ค.
๐vue + Nuxt.js
Vue์์ SSR์ฒ๋ฆฌ๋ฅผ ์ํ ํ๋ ์์ํฌ๋ ๋ฐ๋ก nuxt.js์ด๋ค.
๋์ ๊ฒฝ์ฐ๋ Nuxt๋ฅผ ์ด์ฉํด์ ํ๋ก์ ํธ๋ฅผ ๊ตฌ์ฑ์ ๋ง์ด ํ๊ณ ์๋ค.
ํ์ง๋ง, SSR๋ฅผ ๊ตฌํ์ ํ์ง๋ง ํ์์ ๋ฐ๋ผ์ CSR์ด ํ์ํ ๋ถ๋ถ๋ค์ด ์์๋ค.
์ด๋ด ๋ ์ด์ฉํ๋ ๊ฒ์ด ๋ฐ๋ก <client-only> ์ปดํฌ๋ํธ์๋ค.
<client-only>
<client-only> ์ปดํฌ๋ํธ ์์ ๋ค์ด์๋ ์์ ์์๋ค์ ํด๋ผ์ด์ธํธ ์ฌ์ด๋ ๋๋๋ง์ ์งํํ๊ฒ ๋๋ค.
<template>
<div>
<sidebar />
<client-only placeholder="Loading...">
<!-- this component will only be rendered on client-side -->
<comments />
</client-only>
</div>
</template>
์์ ์์๋ฅผ ๋ณด๋ฉด client-only์์ ์๋ comments๋ CSR๋ก ํธ์ถ์ ํ ์ ์๊ณ
Client Side์์ ๊ทธ๋ ค์ง๋ ๋์ Placeholder์ ์์ฑ๋ ํ ์คํธ๊ฐ ํ์๋๊ฒ ๋ฉ๋๋ค.
<no-ssr>
Nuxt.js v2.9.0 ๋ฒ์ ์ ์๋ <no-ssr>๋ฅผ ์ฌ์ฉํ์ผ๋,
์ ๋ฒ์ ์ด์๋ถํฐ๋ <client-only>๋ก ๋ณ๊ฒฝ๋์์ผ๋,
์ต์ ๋ฒ์ ์์๋ <client-only>๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
๐ฅฒ<client-only> ์์ $ref ๋ฌธ์
<client-only> ์์์ $ref๊ฐ ์์ด์ ํด๋น ์์์ ์ ๊ทผ์ ํ๋ ค๋ค ๋ณด๋ฉด์
๋ฐ๋ก ์ ๊ทผ์ด ๋์ง ์์ ์ฑ undefined๊ฐ ๋จ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
์ด๋ฐ ๊ฒฝ์ฐ๋ Client์์ Rendering์ ํด์ฃผ๊ธฐ ๋๋ฌธ์ mounted๊ฐ ๋๋๋ผ๋
์์ง ๊ทธ๋ ค์ง์ง ์์๋ค๋ฉด ํด๋น ์ปดํฌ๋ํธ๋ฅผ ์์ง ์ฝ์ ์ ์๋ ์ํฉ์ธ ๊ฒ์ด๋ค.
Nuxt์์๋ $netTick์ ์ด์ฉํด์ ์ค๋น๊ฐ ๋ ๋๋ฅผ ์ฒดํฌํด์ ์ ๊ทผ์ ํ๋ ์๋ ์ฝ๋๋ฅผ ์ ์ํ๊ณ ์๋ค.
mounted(){
this.initClientOnlyComp()
},
methods: {
initClientOnlyComp(count = 10) {
this.$nextTick(() => {
if (this.$refs.myComp) {
//...
} else if (count > 0) {
this.initClientOnlyComp(count - 1);
}
});
},
}
mounted ์ initClientOnlyComp๋ฅผ ํธ์ถํด์
this.$ref๊ฐ ์๋์ง ์๋์ง๋ฅผ ์ฒดํฌํ ๋ค ๋ง์ฝ ์๋ค๋ฉด ๋ค์ ํด๋น function์ ์ฌํธ์ถ ํ๋ค.
์ด๊ธฐ countํ์๋งํผ ํด๋น function์ ์ฌ ํธ์ถํ๋ค.
๊ด๋ จ ํฌ์คํ
https://ordinary-code.tistory.com/147
[Vue.js] ref undefined์ผ ๊ฒฝ์ฐ ํด๊ฒฐ ๋ฐฉ๋ฒ (this.$nextTick)
๐ท๏ธVue ref๋? Vue์์ ํน์ ์ปดํฌ๋ํธ๋ HTML์ ์ ๊ทผ์ ํ๊ณ ์ถ์ ๋๋ ref์์ฑ์ ์ด์ฉํด์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค. ๋จผ์ ์๋์ ์ฝ๋๋ก ์์๋ฅผ ๋ณด์. /page/ref.vue ์์์ปดํฌ๋ํธ ์ ๋๋ค. ํ์ ์ปดํฌ๋ํธ ํด๋ฆญ
ordinary-code.tistory.com
Reference
https://nuxtjs.org/docs/features/nuxt-components/
Built-in Components
Nuxt comes with a few important components included out of the box, which will be helpful when building your application.
nuxtjs.org
https://www.toptal.com/vue-js/server-side-rendered-vue-js-using-nuxt-js
Creating Server-side Rendered Vue.js Apps Using Nuxt.js
When initially loading your website, your browser doesn't receive a complete page to display. Instead, it gets a bunch of pieces and instructions of how to put them all together. It takes a substantial amount of time to put all this information together be
www.toptal.com