Vue TypeError _vm不是函数

GGr*_*rec 5 javascript google-maps vue.js vue-component vuejs2

在我的Vue的组件,叫做Home.vue,我包括这谷歌地图插件,如下

<GmapMap
 :center="{lat: 45.919849, lng: 25.0203875}"
 :zoom="7"
   map-type-id="terrain"
  style="width: 100%; height: 600px"
   >
<GmapMarker
:key="markerIdx"
v-for="(m, markerIdx) in results"
:position="getMarkerPosition(m.locationCoordinates)"
:clickable="true"
:draggable="false"
/>
</GmapMap>
Run Code Online (Sandbox Code Playgroud)

该对象results来自父标记,并且m.locationCoordinatesString。该:positionGmapMarker需求JSON对象。我正在定义一个getMarkerPosition函数来将该字符串转换为JSON,就像这样

export default {
      methods: {
   getMarkerPosition: function (coordinateString) {
          let split = coordinateString.split(',')
           return {
            lat: parseFloat(split[0]),
            lng: parseFloat(split[1])
            }
     }
  }
  }
Run Code Online (Sandbox Code Playgroud)

但我最终出现浏览器错误提示

TypeError: _vm.getMarkerPosition is not a function
 at eval (eval at ./node_modules/vue-loader/lib/template-compiler/index.js?            
 {"id":"data-v-8dc7cce2","hasScoped":false,"transformToRequire":{"video": 
  ["src","poster"],"source":"src","img":"src","image":"xlink:href"},"buble": 
 {"transforms":{}}}!./node_modules/vue-loader/lib/selector.js? 
 type=template&index=0!./src/components/Home.vue 
 (0.96557ac29cc33c9d2325.hot-update.js:22), <anonymous>:180:63)
  at Proxy.renderList (vue.esm.js?efeb:3705)
  at Proxy.render (eval at ./node_modules/vue-loader/lib/template- 
  compiler/index.js?{"id":"data-v- 
  8dc7cce2","hasScoped":false,"transformToRequire":{"video": 

 ["src","poster"],"source":"src","img":"src","image":"xlink:href"},"buble": 
 {"transforms":{}}}!./node_modules/vue-loader/lib/selector.js? 
  type=template&index=0!./src/components/Home.vue 
 (0.96557ac29cc33c9d2325.hot-update.js:22), <anonymous>:173:47)
   at VueComponent.Vue._render (vue.esm.js?efeb:4544)
   at VueComponent.updateComponent (vue.esm.js?efeb:2788)
   at Watcher.get (vue.esm.js?efeb:3142)
   at Watcher.run (vue.esm.js?efeb:3219)
   at flushSchedulerQueue (vue.esm.js?efeb:2981)
   at Array.eval (vue.esm.js?efeb:1837)
   at flushCallbacks (vue.esm.js?efeb:1758)```
Run Code Online (Sandbox Code Playgroud)

整个代码在中Home.vue。我只Vue.use(VueGoogleMaps)main.js

GGr*_*rec 6

我想分享我的解决方案,以防有人遇到类似问题。

主要问题是我GmapMap的父组件是另一个名为ais-results(来自Vue InstantSearch)的组件,它具有该inline-template属性。这意味着标签内的任何内容都ais-results无法看到“外部”。

我优雅的解决方案是将其提取GmapMap到一个单独的组件/文件中,我可以在其中定义所有所需的方法。当我使用新组件时,我只需将数据作为props.

另一种解决方案是避免该inline-template属性。

相关问题和资源:

  1. Vue:方法不是内联模板组件标记中的函数
  2. Vue 内联模板找不到方法或数据
  3. https://medium.com/js-dojo/7-ways-to-define-a-component-template-in-vuejs-c04e0c72900d
  4. https://community.algolia.com/vue-instantsearch/components/results.html

请记住,我是反应性和 Vue 方面的初学者。干杯