Vue 고급 트릭 치트 시트

11948 단어 javascriptvue
안녕하세요 DEV.to 커뮤니티입니다!

저는 평소에 Vue와 관련되어 있기 때문에 해결해야 할 많은 문제에 직면하고 있으며 개발자에게 오랫동안 짜증나는 문제를 해결하는 것보다 더 좋은 느낌은 없다고 생각합니다! xD

그래서 여기에서 몇 가지 문제를 극복하기 위해 사용했던 몇 가지 트릭을 여러분과 공유하고 있으며 언젠가는 여러분에게도 유용할 것입니다.

https://qiita.com/_masa_u/items/6599546a7f3db2e2ffbc에 이 기사의 일본어 버전이 있습니다.


.ltag__user__id__378977 .follow-action-button {
배경색: #ffffff !important;
색상: #f50000 !중요;
테두리 색상: #f50000 !중요;
}



유 마사



Software Developer working mainly with Python, Javascript



즐기다!

내용의 테이블


  • Force re-rendering
  • Watching nested data
  • Custom components with v-model support
  • Functional component

  • 강제 재렌더링

    In some cases, it is necessary to force Vue in order to re-render your component since it won't track some changes.

    There are some ways of doing so, such as using the v-if hack, but the correct way is by using forceUpdate method:

      export default {
        methods: {
          forceUpdateMyComponent() {
            this.$forceUpdate()
          },
        },
      }
    

    Keep that in mind that $ is necessary to use this method in your Vue instance.
    And you better be notified that this won't update any computed property but only force the view of your component to be re-rendered.

    중첩 데이터 보기

    Sometimes you might need to watch some nested data you can do this using dot notation:

      export default {
        data() {
          return {
            myInfo: {
              firstName: 'Adnan'
            }
          }
        },
        watch: {
          'myInfo.firstName'() {
            console.log('Here');
          }
        }
      }
    

    The code above is when you know which property inside your object to watch!
    But what if you want to watch your whole object and also its values?

      export default {
        data() {
          return {
            myFruits: {apple: 'red', banana: 'yellow'},
          }
        },
        methods: {
          changeIt() {
            this.myFruits.apple = 'green'
          },
        },
        watch: {
          myFruits: {
            deep: true,
            handler() {
              console.log('Fruits updated!')
            },
          },
        },
      }
    

    You can define your watch not as a function immediately but as an object, then by using the deep key and the value true watch your object and put what you want to be fired when the data got changed inside a function called handler .

    v-model을 지원하는 사용자 지정 구성 요소

    As you know v-model is used to make a two-way binding with an input element/component and a data property.

    If you are making a custom component and want it to support v-model you can emit the input keyword and in order to receive what is passed to your component initially with the v-model you simply need to get a prop called value .

    For an easier comprehension check the code below:

    <template>
        <div>
            <label>My custom test input: <input :value="inputValue" @input="emitTheData" /></label>
        </div>
    </template>
    
    <script>
      export default {
        props: ['value'],
        data() {
          return {
            inputValue: value
          }
        },
        methods: {
          emitTheData() {
            this.$emit('input', this.inputValue)
          }
        }
      }
    </script>
    

    This simple component will bind the data passed to it in two directions.

    Note: As you can see, I assigned the prop called value to a data called inputValue and used it in my input, you might think that I could pass the prop directly to my input but as Vue suggests it is better not to alter props directly.

    기능성 성분

    Imagine having a component with no watch or computed property and no methods! You would only use it to render some other components as one with some props. If it is so then why waste Vue's time when there are no lifecycle methods?

    A functional component can be defined as below:

    <template functional>
      <div>{{ props.foo }}</div>
    </template>
    

    Note: Keep in mind that there is no this context in these components since it is a functional component!

    You can check the link below for more information about functional components in Vue:
    Vue functional components

    Vue 앱을 더 빠르게 만들거나 개발 속도를 더 빠르게 하기 위해 사용하는 다른 트릭이 있으면 알려주세요. 여기에 추가하겠습니다.

    여기에서 무료 Node.js Essentials 전자책을 확인하세요.


    좋은 웹페이지 즐겨찾기