VueJS设置输入字段数据

VueJS set Input field data

本文关键字:数据 字段 输入 设置 VueJS      更新时间:2023-09-26

当我从另一个函数接收更新信息时,我想用VueJS设置输入字段的值。我不清楚,当函数getLatLng返回时,如何更新输入字段的值。当我在控制台上输出时,它工作得很好,所以功能正常。但是如何正确连接输入字段以显示Latitude值呢?我需要计算财产吗?还是普通的?

HTML

<form>
<input type="text" class="Form" placeholder="Latitude" v-model="latitude" value="@{{ latitude }}">
</form>

VUE

var Vue = require('vue');
import VueResource from 'vue-resource';
Vue.use(VueResource);
window.app = new Vue({
    el: '#GISContainer',
    // Data to be stored
    data: {
        // Save Data for address, latitude and longitude
        address: '',
        latitude: '',
        defaultMapLocation: 'Fichtenstrasse 30 82256',
        // Responder Information
        responders: [
            {id: 1, name: 'Test 1'},
            {id: 1, name: 'Test 2'},
        ]
    },
    // Methods
    methods: {
        init: function() {
            initGISMap(this.$els.map);
            this.address === '' ? setMapLocationToAddress(this.defaultMapLocation) : setMapLocationToAddress(this.address);
        },
        setMapToSearchAddress: function() {
            setMapLocationToAddress(this.address);
            getLatLng(this.address, function(latlng) {
                this.latitude = latlng[0];
            })
        }
    }
});

外部JS函数

function geocodeAddress(address, callback) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ address: address }, function(results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            if (results.length > 0) {
                callback(results[0].geometry.location);
            } else {
                console.log("No results found");
            }
        } else {
            console.log("Geocoder failed due to: " + status);
        }
    });
}
function getLatLng(address, callback) {
    latLng = [];
    geocodeAddress(address, function(position) {
        latLng.push(position.lat());
        latLng.push(position.lng());
        callback(latLng);
    });
}

要设置latitude,您应该绑定this关键字:

getLatLng(this.address, function(latlng) {
  this.latitude = latlng[0];
}.bind(this))