sourcecode

Vue.js의 컴포넌트 라이프 사이클 메서드에서 믹스인 메서드 내 기능에 액세스하는 방법

copyscript 2022. 8. 12. 23:34
반응형

Vue.js의 컴포넌트 라이프 사이클 메서드에서 믹스인 메서드 내 기능에 액세스하는 방법

다음은 예를 제시하겠습니다.

mixin.displaces

export default {
    methods : {
        aFunction() { // Some functionality here }
    }
}

요소.표시하다

import mixin from './mixin'
export default {
    mixins : [ mixin ]
    created() {
        // Call aFunction defined in the mixin here
    }
}

컴포넌트 내의 created() 라이프 사이클 메서드에서 mixin 메서드에 정의된 aFunction에 액세스하고 싶습니다.

mixin 메서드는 컴포넌트의 현재 인스턴스와 병합되므로 다음과 같습니다.

created(){
  this.aFunction()
}

여기 예가 있습니다.

console.clear()

const mixin = {
  methods:{
    aFunction(){
      console.log("called aFunction")
    }
  }
}

new Vue({
  mixins:[mixin],
  created(){
    this.aFunction()
  }
})
<script src="https://unpkg.com/vue@2.4.2"></script>

언급URL : https://stackoverflow.com/questions/46413319/how-to-access-function-inside-methods-of-mixin-from-component-lifecycle-method-i

반응형