V
V
vitaly_742021-04-27 13:54:37
Vue.js
vitaly_74, 2021-04-27 13:54:37

How to make vue component decorator?

now there is a mapbox component and it works great

<mapbox
      access-token="your access token"
      :map-options="{
        style: 'mapbox://styles/mapbox/light-v9',
        center: [-96, 37.8],
        zoom: 3,
      }"
      :geolocate-control="{
        show: true,
        position: 'top-left',
      }"
      @map-load="loaded"
      @map-zoomend="zoomend"
      @map-click:points="clicked"
      @geolocate-error="geolocateError"
      @geolocate-geolocate="geolocate"
    />

now I want to add polygon drawing capability here, and add MapBoxDraw component
here is my main.js
import Vue from 'vue'
import VueApexCharts from 'vue-apexcharts'
import Mapbox from 'mapbox-gl-vue'
import MapBoxDraw from 'mapbox-gl-draw'

window.Vue = Vue;
window.Vue.component('apex-charts', VueApexCharts);
window.Vue.component('map-box', Mapbox);
window.Vue.component('map-box-draw', MapBoxDraw);

I run it all through php widgets and insert it on the site in the form of configurations. for example here is how it looks for MapBox
<?= VueWidget::widget([
    'component'=>'map-box',
    'property'=>[
        'style'=>[
            'height'=> "500px"
        ],
       'access-token'=>'',
       ":map-options"=>[
           "style"=>'mapbox://styles/vitaly2litvino/cknpuyit02f8g17o1r9mnqra8',
           'center'=>[-68.137343, 45.137451],
           'zoom'=>10
       ],
        ':fullscreen-control'=>[
            "show"=>false,
            'position'=> 'top-left'
        ],
        "@map-init"=>"loaded" //вот это меня не устраивает
    ]
])?>

in this case, to add polygon drawing, I have to insert the js loaded method into PHP - which is not very good, I think it's not convenient, and configure MapBoxDraw in it.
I would like to create a MapBoxWithDraw vue component
that will be initialized in the same way as the MapBox, only with an additional property.
for example: draw=>[]....
and in the component itself, describe the necessary methods, and pass data to MapBoxDraw. (For the future, I will also transfer data from php).
<script>
    import Mapbox from 'mapbox-gl-vue'
    import MapBoxDraw from 'mapbox-gl-draw'

    export default {
        components: { Mapbox },
        props: {
            "map-options": {
                "style": "mapbox://styles/vitaly2litvino/cknpuyit02f8g17o1r9mnqra8"
            },

        },
        methods: {
            initialized(map) {
                const Draw = new MapBoxDraw();
                map.addControl(Draw)
            },
        },
    }
</script>

The question is how to implement such a decorator pattern, I tried to do this but it does not work.

Please tell me how to implement it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question