【问题标题】:VueJS Mixins MethodsVueJS 混合方法
【发布时间】:2016-05-10 23:35:48
【问题描述】:

我想在我的 VUEJS 模块中使用 mixin:

模块

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

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

混音

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 Autocomplete for GIS
            initAutoComplete();
        }
    }
}

但我收到一个错误,即 GoogleMaps.initGISMap 不是函数。如何在组件中使用 mixin 的方法?

【问题讨论】:

  • 我相信你需要用这个来引用 mixin。所以在模块 this.GoogleMaps.initGISMAP(data)

标签: javascript vue.js


【解决方案1】:

-- 编辑以纠正我在解释您的需求时所犯的错误

当使用 mixin 时,你不会引用方法 MixinName.method() - 它只是 'this' - 你的 mixin 返回的那些方法和属性是一阶的,可以这么说,所以它们绑定到 'this '。

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

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

【讨论】:

猜你喜欢
  • 2017-10-22
  • 2018-02-17
  • 2014-01-26
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
相关资源
最近更新 更多