javascript - How can I use a Global Mixin method from a Vue instance - Stack Overflow

Let's suppose that I have the following situation, using a Global Mixin to create a global helper

Let's suppose that I have the following situation, using a Global Mixin to create a global helper method with Vue:

import Vue from "vue";

Vue.mixin({
    methods: {
        replaceString: function (word) {
            return word.toLowerCase().replace(/\W/g, '');
        }
    }
});

let vm = new Vue({
    methods: {
        doSomething: function() {
             console.log(this.replaceString('Hello World'); //helloword
        }
    }
});

I know that I can invoke the method inside the other methods, inside of the ponent and their childs. But how can I invoke the mixin method "replaceString" from the Vue instance "vm"? I tried to use "vm.replaceString", but keeps returning "undefined".

Let's suppose that I have the following situation, using a Global Mixin to create a global helper method with Vue:

import Vue from "vue";

Vue.mixin({
    methods: {
        replaceString: function (word) {
            return word.toLowerCase().replace(/\W/g, '');
        }
    }
});

let vm = new Vue({
    methods: {
        doSomething: function() {
             console.log(this.replaceString('Hello World'); //helloword
        }
    }
});

I know that I can invoke the method inside the other methods, inside of the ponent and their childs. But how can I invoke the mixin method "replaceString" from the Vue instance "vm"? I tried to use "vm.replaceString", but keeps returning "undefined".

Share Improve this question asked Feb 15, 2019 at 19:18 Marcelo RodovalhoMarcelo Rodovalho 9331 gold badge16 silver badges27 bronze badges 1
  • It says at docs that you can transform it into a ponent, so you would just import it and use. Take a look at the docs: vuejs/v2/guide/mixins.html – Marco Commented Feb 15, 2019 at 20:28
Add a ment  | 

2 Answers 2

Reset to default 2

Few changes to your code and it works:

  1. You should change the definition of your mixin (var mixin instead of Vue.mixin)
  2. Import the mixin to your new vue ponent (mixins = [mixin])

    import Vue from "vue";
    
    var mixin = {
        methods: {
            replaceString: function (word) {
                return word.toLowerCase().replace(/\W/g, '');
            }
        }
    };
    
    let vm = new Vue({
        mixins: [mixin]
        methods: {
            doSomething: function() {
                console.log(this.replaceString('Hello World'); //helloword
            }
        }
    });
    

I think this chunk o code is what you are looking for:

var mixin = {
methods: {
foo: function () {
  console.log('foo')
},
   conflicting: function () {
      console.log('from mixin')
   }
 }
}

var vm = new Vue({
  mixins: [mixin],
methods: {
  bar: function () {
  console.log('bar')
  },
    conflicting: function () {
      console.log('from self')
    }
  }
})

vm.foo() // => "foo"
vm.bar() // => "bar"
vm.conflicting() // => "from self"

From the docs

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745331355a4622892.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信