Trying to add geocomplete to a Vue js webpack project

I've added Geocomplete to a Vue project before but not with Webpack. I tried using the npm module, which requires the following in index.html:

<script src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
<script src="jquery.geocomplete.js"></script>

(of course, I filled in my api key and it's valid)

When I try to add geocomplete to an input in my App.vue file:

   $("address-input").geocomplete();

I get the message:

TypeError: __WEBPACK_IMPORTED_MODULE_5_jquery___default(...)(...).geocomplete is not a function

Any ideas how to use this module with Vue?

Here's my index.html file:

<!DOCTYPE html>
<html>
   <head>

        <script src="http://maps.googleapis.com/maps/api/js?libraries=places&key=mykey"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 

      <script src="https://cdnjs.cloudflare.com/ajax/libs/geocomplete/1.7.0/jquery.geocomplete.js"></script>

    <meta charset="utf-8">
    <title>gotta-go</title>
    <style ></style>


  </head>

  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

webpack.dev.conf.js:

'use strict'
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')

// add hot-reload related code to entry chunks
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
  baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
})

module.exports = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
  },
  // cheap-module-eval-source-map is faster for development
  devtool: '#cheap-module-eval-source-map',
  plugins: [
    new webpack.DefinePlugin({
      'process.env': config.dev.env
    }),
    // https://github.com/glenjamin/webpack-hot-middleware#installation--usage
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    // https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      inject: true
    }),
    new FriendlyErrorsPlugin()
  ]
})

Here is my app.vue

<template>
  <div>
                    <el-form ref="form" :model="form" label-width="120px">
                        <el-form-item label="Location">                            
                            <el-input id="address-input"></el-input>
                        </el-form-item>                        
                        <el-form-item label="Distance">
                            <el-slider v-model="form.distance"></el-slider>
                        </el-form-item>
                        <el-form-item label="Rating">
                            <el-rate void-color="rgb(100, 100, 100)" v-model="form.rating"></el-rate>
                        </el-form-item>


                        <el-form-item label="special:">                        
                            <el-checkbox-group v-model="form.specials">

                            </el-checkbox-group>                        
                        </el-form-item>                    
                    </el-form>

  </div>

</template>
<script>

 import app from './db.js'


 import Icon from 'vue-awesome/components/Icon'
 import L from 'leaflet'
 import Vue2Leaflet from 'vue2-leaflet'

 var db = app.database()
 var storage = app.storage()
 var usersRef = db.ref('users')
 var toiletsRef = db.ref('toilets')
 var auth = app.auth()



 export default {
     name: 'app',
     components: {         
         Icon,
         'v-map': Vue2Leaflet.Map,
         'v-tilelayer': Vue2Leaflet.TileLayer,
         'v-marker': Vue2Leaflet.Marker         

     },
     mounted(){

         $("#address-input").geocomplete();

     },
    }
</script>

Answers 1

  • I tried to replicate the problem, but geocomplete worked fine.

    All i did was:

    1. Create project using vue-cli. vue init webpack my-project and npm i.
    2. Enable Google Map APIs.
    3. Edit index.html and App.vue as below.

    index.html

      <head>
        <meta charset="utf-8">
        <title>vue-webpack-playground</title>
        <script src="http://maps.googleapis.com/maps/api/js?libraries=places&key=MYAPIKEY"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
        <script src="https://cdnjs.cloudflare.com/ajax/libs/geocomplete/1.7.0/jquery.geocomplete.js"></script>
      </head>
    

    App.vue

    <template>
      <div id="app">
        <input type="text">
      </div>
    </template>
    
    <script>
    export default {
      name: 'app',
      mounted: function () {
        $("input").geocomplete();
      }
    }
    </script>
    

    Result

    enter image description here


Related Articles