Vue.js에서 Selectbox 컴포넌트 만들기

by HeuJung


개발 | Posted on Mon, Jan 7, 2019 4:59 PM
1

Vue.js에서 입력 폼을 만들 때 사용할 수 있는 Selectbox 컴포넌트를 만드는 방법을 소개하고자 한다.

SelectBox.vue (이름은 자유롭게 정한다) 파일을 하나 만들고 template를 아래와 같이 작성한다.

<template>
    <select
          v-model="this.value"
          class="custom-select"
          v-bind:id="input_id"
          v-on:input="updateValue($event.target.value)"
    >
        <option v-for="(item, index) in items" :value="index">{{ item }}</option>
    </select>
</template>

Vue.js 2.2 버전부터 v-model을 사용하는 컴포넌트는 value props를 가진다. (참고: #) 따라서 별도로 v-bind로 props를 내려줄 필요 없이 하위 컴포넌트에서 this.value라고만 명시해주면 상위 컴포넌트의 value 값을 하위 컴포넌트 값으로 가져올 수 있다.

그리고, v-on:input="updateValue($event.target.value)" 부분은 사용자가 selectbox의 값을 변경할 경우 변경된 값을 상위 컴포넌트로 전송하기 위한 코드이다. 이 코드가 없으면 상위 컴포넌트에 사용자가 변경한 값이 반영되지 않으니 주의하여야 한다.

script는 아래와 같이 작성한다.

<script>
    export default {
        name: "SelectBox",

        data() {
            return {
                //
            };
        },

        props: ['value', 'items', 'input_id'],

        methods: {
            updateValue: function (value) {
                this.$emit('input', value);
            }
        }

    }
</script>

updateValue 메서드는 사용자가 입력한 값을 input 이벤트로 내보내는 역할을 한다.

마지막으로, 컴포넌트를 가져다 쓰는 부분의 코드는 아래와 같다.

<SelectBox
       v-model="preselect_value"
       :items="somethings"
       :input_id="'my_selectbox'"
       @input="value => { preselect_value = value }"
></SelectBox>

 

Vue.js Component Develop


Leave a Comment:
LorenaBlairjoupe 11 hours ago Edit Delete Reply
Four dating habits to cure doing now as told in GIFs If you noticed a growing sense of cynicism with dating, You not by yourself. Go out for lunch with a group of single friends like I did recently, And the conversation will inevitably turn to a discussion of everything that apparently wrong with modern relationships. While technology has made it more convenient for is to connect in some ways, It also lead many of us to cultivate some bad habits. In the mood of change, Here are a few things we need to stop doing right now.1. Keeping people the hook a answer why the show How I Met Your Mother devoted an entire episode to the phenomenon we all either been on the hook, Or had someone on the hook at some point in our dating careers. with regards to someone the hook it means you <a href=https://www.bestbrides.net/asiafriend-finder-review-for-2020-scam-or-good-for-finding-ture-love/>asian friend finder</a> not interested in dating them, But keep them around in the event. for example, years back I dated a guy who claimed to have feelings for me however, Whenever I brought up the main topics our future together, He tell me that he just be in a spousal relationship right now. If someone wants to be with you, They may make it happen. I can help but think how much time that guy could possibly have saved me if he just told me the truth: That he didn see a future conjointly. at. It time we all cut the crap and get real with those who in our lives. (that brings to mind, If you into someone who says it makes them with you now it time to move on. You have earned better. individuals do.)2. GhostingReal, obsolete breakups are becoming an endangered species. I used to live in fear of being broken up with, so now, I actually grateful somebody takes the time to tell me they no longer want to see me instead of simply disappearing into thin air (that's why, businesses Sure, Sometimes it can feel like the kindest thing to say is nothing at all specifically if you don know the person very well. bear in mind, If you been dating someone for a time and have seen each other naked, You owe them some type of explanation. This season I was dating a man who, After a month of going out, Simply faded. When he sent me a thoughtful text apologizing several months later, My solution was, Could have told me this several weeks ago. I could have been fine. knock back stings, But it also allows us to move on with a clean slate.3. Body part photosI be lying if I said I didn enjoy receiving the odd sexy photo sometimes. and yet, The sending and receiving of body part photos has gotten a bit unable to cope. It reached the point where I receiving photos of men genitals before I even seen them in real life. online dating sites are starting to resemble literal meat markets: Just a collection of anonymous pecs, Biceps and rippled bellies. affiliate marketers, this might come as a surprise, But I much rather learn about your interests and see your gorgeous mug than have you text me that photo you took of your abs at a really weird angle. You far better this. We are all.4. Not owning up to what you want wantNot happy with your love life? you have the power to change it. It starts by being honest with ourselves. If you looking for a serious relationship, Stop having casual sex when you know it doesn make you feel good. If you know monogamy doesn meet your needs, Stop getting into monogamous romance. If something great in your pants, But harmful in your heart, Stop doing that concern. Your fancies are valid. Have the courage to recognise them.