Vue

๐Ÿณ HOC์‚ฌ์šฉํ•˜์—ฌ ๋กœ์ง ๊ณตํ†ตํ™” ์‹œํ‚ค๊ธฐ

frontChoi 2023. 11. 5. 20:26
๋ฐ˜์‘ํ˜•

๐Ÿฅ™ ๊ธฐ๋Šฅ ๊ณตํ†ตํ™” ์ƒํ™ฉ

์•„๋ž˜ ์‚ฌ์ง„ ์ฒ˜๋Ÿผ  "ํ…Œ์ŠคํŠธ" ๋ผ๋Š” ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด '๋ชจ๋˜,๋Ÿญ์…”๋ฆฌ' ์™€ '1990,2000s' validation ๊ธฐ๋Šฅ์ด ์ค‘๋ณต์ด ๋œ๋‹ค.

์‹ค์ œ ์‹ค๋ฌด์—์„œ๋Š” ๋กœ์ง์ด ์ด๊ฒƒ๋ณด๋‹ค ๋” ๋ณต์žกํ•˜๊ฒŒ ๋Œ์•„๊ฐ€์ง€๋งŒ, ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋นˆ๊ฐ’ ์ฒดํฌ ์ •๋„๋งŒ ๊ธฐ๋Šฅ์„ ๋„ฃ์—ˆ๋‹ค.

ํ๋ ˆ์ด์…˜

 

๐Ÿฅ™ ์ „์ฒด์ ์ธ ๊ตฌ์กฐ

withCuration์„ ํ†ตํ•ด validation์˜ ๊ธฐ๋Šฅ์„ props์œผ๋กœ ์ „๋‹ฌํ•˜๊ณ , props์œผ๋กœ ์ „๋‹ฌ ๋ฐ›์€ ์ปดํผ๋„ŒํŠธ๋“ค์€ validation์„ ํ†ตํ•ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐ŸŸ withCuration.js ์ž‘์„ฑ

const withCuration = (component) => {
  return {
    name: "withCuration",
    data() {
      return {
        curationInfo: {
          mood: [
            {
              text: "๋ชจ๋˜",
              value: 1
            },
            {
              text: "๋Ÿญ์…”๋ฆฌ",
              value: 2
            }
          ],
          peroid: [
            {
              text: "1990",
              value: 1
            },
            {
              text: "2000s",
              value: 2
            }
          ]
        }
      }
    },
    created() {

    },
    mounted() {
    },
    methods: {
      validation(
        curation = {
          mood: [],
          peroid: [],
          moodAlert: () => { },
          peroiAlert: () => { }
        },
        validateFn = () => { }
      ) {
        if (curation.mood.length === 0) {
          curation.moodAlert();
          return false;
        } else if (curation.peroid.length === 0) {
          curation.peroiAlert();
          return false;
        }
        validateFn();
      }
    },
    render(createElement) {
      return createElement(component, {

        props: {
          curationinfo: this.curationInfo,
          validate: this.validation
        },
      })
    }
  }
}
export { withCuration }

props์—์„œ ํ๋ ˆ์ด์…˜ ๊ฐ’๋“ค์„ ๋˜์ ธ์ค€๋‹ค ์—ฌ๊ธฐ์„œ๋Š” mood(๋ถ„์œ„๊ธฐ)์™€ , peroid๋ฅผ ์ „๋‹ฌํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  validate๋ผ๋Š” ๊ฒƒ์„ ์ „๋‹ฌํ•ด์ฃผ๋ฉฐ, ์ด๊ฒƒ์€ ํ๋ ˆ์ด์…˜์ด ๋นˆ๊ฐ’์„ ์ฒดํฌํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

 

 

๐Ÿฅ— withCuration.js ์ƒ์† ๋ฐ›๋„๋ก ์ž‘์„ฑ

 

curationView.vue

import { withCuration } from "@/hoc/withCuration";
import curationView from '@/views/userLayout/curationView.vue'


const routes = [
      {
        path: "/curation",
        name: "curationView",
        component: withCuration(curationView)
      }
]

 

CurationComp.vue

import CurationComp from '@/components/CurationComp.vue';
import { withCuration } from '@/hoc/withCuration';

export default {
	components: {
    	'curation-comp': withCuration(CurationComp)
    }
}

 

curationView.vue์™€ CurationComp.vue ๋Š” withCuration์„ ํ†ตํ•ด์„œ ์ƒ์†์„ ๋ฐ›๋Š” ๋‹ค ๊ทธ๋Ÿฌ๋ฉด vue-devtools์—์„œ ์•„๋ž˜์‚ฌ์ง„์ฒ˜๋Ÿผ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

vue-devtools

 

 

๐Ÿง† ์ž์‹์ปดํผ๋„ŒํŠธ์—์„œ withCuration ํ•จ์ˆ˜ ํ˜ธ์ถœ

<template>
  //๊ธฐ๋Šฅ์ด ์ค‘์š”ํ•˜๋ฏ€๋กœ, template์€ ์ƒ๋žตํ•œ๋‹ค.
</template>
<script>
export default {
  name: 'CurationComp',
  props: {
    validate: {
      type: Function,
      default() {
        return 'Default function'
      }
    },
    curationinfo: {
      type: Object,
      default: () => {
        return {}
      }
    },

  },
  data() {
    return {
      mood: [],
      peroid: [],
    };
  },

  mounted() {

  },

  methods: {
    clickTest() {
      const send = {
        mood: this.mood,
        peroid: this.peroid,
        moodAlert: () => {
          alert('๋ถ„์œ„๊ธฐ๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”.')
        },
        peroiAlert: () => {
          alert('์‹œ๋Œ€๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”.')
        }
      }
      //1.pros์œผ๋กœ ์ „๋‹ฌ๋ฐ›์€ validate๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
      //2.send์—๋Š” mood,peroid,moodAlert,peroiAlert ๋ฅผ ํ†ตํ•ด validate์ธ์ž์— ๋„ฃ๋Š”๋‹ค.
      //2-1.moodAlert์€ moodAlert์€ validation์„ ์‹คํŒจํ–ˆ์„๋•Œ peroiAlert์€ peroid๊ฐ€ ๋นˆ๊ฐ’์ผ๋•Œ ๋™์ž‘ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
      this.validate(send, this.alertValidate);
    },
    alertValidate() {
      alert(`validation์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค.`)
    }
  },
};
</script>
<style scoped src="@/assets/styles/modal.css"></style>

validateํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , mood,peroid,moodAlert,peroidAlert๋ฅผ ์ „๋‹ฌํ•˜๋ฉด moodAlert,peroidAlert์€ mood์™€ peroid๊ฐ€ ๋นˆ๊ฐ’์ด๋ฉด ๋™์ž‘ํ•˜๋Š” ํ•จ์ˆ˜ ์ด๋‹ค.

 

๐Ÿซ•  ํ›„๊ธฐ

hoc๋ฅผ ํ†ตํ•ด ๊ธฐ๋Šฅ์„ ๊ณตํ†ตํ™” ์‹œ์ผฐ์ง€๋งŒ, hoc๋Š” ์ปดํผ๋„ŒํŠธ ๊นŠ์ด๊ฐ€ ๊นŠ์–ด์ง„๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. mixin์„ ์“ธ๋ ค๋‹ˆ ๋ฐ์ดํ„ฐ๋‚˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ฒน์น˜๋ฉด ๋ฎ์–ด์”Œ์šฐ๊ธฐ๊ฐ€ ๋œ๋‹ค. ํ”„๋กœ์ ํŠธ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์ด๊ฒƒ์€ ์ถ”์ฒ™์ด ์–ด๋ ต๊ธฐ๋•Œ๋ฌธ์—, hoc๊ฐ€ ์ปดํผ๋„ŒํŠธ ๊นŠ์ด๊ฐ€ ๊นŠ์–ด์ง„๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์„ ํƒํ•˜์˜€๋‹ค.

๋ฐ˜์‘ํ˜•