最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access function inside methods of mixin from component lifecycle method in Vue.js - Stack Overflow

programmeradmin0浏览0评论

Here is an example :

mixin.js

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

ponent.vue

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

I want to access the aFunction defined inside methods of mixin from the created() lifecycle method inside the ponent.

Here is an example :

mixin.js

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

ponent.vue

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

I want to access the aFunction defined inside methods of mixin from the created() lifecycle method inside the ponent.

Share Improve this question asked Sep 25, 2017 at 20:04 Gaurav SarmaGaurav Sarma 2,2972 gold badges28 silver badges46 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

The mixin methods are merged with the current instance of the ponent, so it would just be:

created(){
  this.aFunction()
}

Here is an example.

console.clear()

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论