1

我有一个 vuejs 表单和一个纯 JS 的地图小部件。我需要从地图(vanilla js)中提取坐标到表单输入(vuejs)中。我怎样才能做到这一点?看法:

<el-form :model="storeForm" label-width="200px" ref="storeForm" status-icon size="small">
    <el-form-item label="Address" prop="Address">
        <el-input v-model="storeForm.Address" type="textarea"></el-input>
    </el-form-item>
    <el-form-item>
        <div id="map" style="width: 600px; height: 400px"></div>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" @click="save('storeForm')">Save</el-button>
        <el-button @click="cancel()">Cancel</el-button>
    </el-form-item>
</el-form>

还有一个 vuejs 代码:

export default {
    data() {
        return {
            isNew: true,
            storeForm: {
                Address: ''
            }
        }
    },
    created() {
        ymaps.ready(function () {
            var map = new ymaps.Map("map", {
                center: [43.241203, 76.957206],
                zoom: 13
            });

            map.events.add('click', (e) => {
                var coords = e.get('coords');

                //obviously that doesn't work
                this.storeForm.Address = e.get('coords');
            });
        });
    }
}
4

1 回答 1

0

线

ymaps.ready(function () {

创建一个新范围,因此this不再是您的 Vue 应用程序。

用。。。来代替

ymaps.ready(() => {

.bind(this)在该函数的末尾添加。

于 2018-01-12T16:59:44.797 回答