首页 > 解决方案 > this.$router.push() 在谷歌地图 place_changed 函数中无法识别

问题描述

我有一个连接到谷歌地图位置自动完成功能的文本框。我调用了新的google.maps.places.Autocomplete()内部 vue 安装属性。根据搜索结果,我想将我的页面重定向到调用 this.$router.push() 函数的搜索过滤器页面。但我不知道为什么this.$router.push()在 google map 内部无法识别place_changed()

this.$router.push()我在里面打电话mounted(){}。它工作正常,但我需要识别this.$router.push()里面place_changed()

<input id="modal-filters-Main"
                                        placeholder="Enter an address, neighborhood, city or ZIP code"
                                        class="typeahead form-control form-ready-for-bubble" >

var SearchBox = new google.maps.places.Autocomplete(document.getElementById('modal-filters-Main'));
            SearchBox.addListener('place_changed', function() {
                 this.place = SearchBox.getPlace();
                 this.formatter = this.place.formatted_address;
                 this.lower = this.formatter.toLowerCase();
                 this.modified = this.lower.replace(", ","_");
                this.$router.push('/listing/'+this.modified);
                // window.location.href = '/listing/'+modified;
            });

标签: google-mapslaravel-5vuejs2

解决方案


如果你想保留你的代码,你可以在你的函数中添加它。

让 $this = 这个

但它似乎又脏又旧,最好将函数更改为 ()=>


推荐阅读