最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - VueJS Mixins Methods - Stack Overflow

matteradmin4PV0评论

I want to use a mixin within my VUEJS module:

Module

<script>
    var GoogleMaps = require('../mixins/GoogleMaps');

    export default {
        mixins: [GoogleMaps],
        events: {
            MapsApiLoaded: function(data) {
                GoogleMaps.initGISMap(data);
            }
        },
}
</script>

Mixin

export default {
    methods: {
        initGISMap(selector) {
            map = new google.maps.Map(selector, {
                zoom: 10,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
            });

            // Set initial Location and center map to this location
            initialLocation = new google.maps.LatLng(48.184845, 11.252553);
            map.setCenter(initialLocation);

            // Create a searchmarker
            searchMarker = createMarker();

            // Init Autoplete for GIS
            initAutoComplete();
        }
    }
}

But I get an error, that GoogleMaps.initGISMap is not a function. How do I use a method of a mixin within a ponent?

I want to use a mixin within my VUEJS module:

Module

<script>
    var GoogleMaps = require('../mixins/GoogleMaps');

    export default {
        mixins: [GoogleMaps],
        events: {
            MapsApiLoaded: function(data) {
                GoogleMaps.initGISMap(data);
            }
        },
}
</script>

Mixin

export default {
    methods: {
        initGISMap(selector) {
            map = new google.maps.Map(selector, {
                zoom: 10,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
            });

            // Set initial Location and center map to this location
            initialLocation = new google.maps.LatLng(48.184845, 11.252553);
            map.setCenter(initialLocation);

            // Create a searchmarker
            searchMarker = createMarker();

            // Init Autoplete for GIS
            initAutoComplete();
        }
    }
}

But I get an error, that GoogleMaps.initGISMap is not a function. How do I use a method of a mixin within a ponent?

Share Improve this question edited Jun 15, 2016 at 7:59 Thomas Ayoub 29.5k16 gold badges98 silver badges147 bronze badges asked May 10, 2016 at 23:35 sesc360sesc360 3,28510 gold badges46 silver badges91 bronze badges 1
  • i believe you need to reference the mixin with this. so in module this.GoogleMaps.initGISMAP(data) – vbranden Commented May 11, 2016 at 19:57
Add a ment  | 

1 Answer 1

Reset to default 7

-- edit to correct mistake I made in interpreting your needs

When using mixins, you don't reference the methods MixinName.method() - it's just 'this' - those methods and properties returned by your mixin and are first order, so to speak, so they are bound to 'this'.

<script>
    var GoogleMaps = require('../mixins/GoogleMaps');

    export default {
        mixins: [GoogleMaps],
        events: {
            MapsApiLoaded: function(data) {
                this.initGISMap(data);
            }
        },
}
</script>
Post a comment

comment list (0)

  1. No comments so far