如何从vuejs中的javascript导出数据?

时间:2018-01-12 15:42:33

标签: javascript vue.js yandex-maps

我有一个vuejs表单和一个纯粹的基于JS的地图小部件。我需要从地图(vanilla js)中提取coords到表单输入(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');
            });
        });
    }
}

1 个答案:

答案 0 :(得分:0)

该行

ymaps.ready(function () {

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

替换为

ymaps.ready(() => {

或在该功能的末尾添加.bind(this)