尝试将地理填充添加到Vue js webpack项目中

时间:2017-10-29 03:43:37

标签: jquery webpack vue.js geocomplete

我之前已将Geocomplete添加到Vue项目中,但未将其添加到Webpack中。我尝试使用npm module,这需要index.html中的以下内容:

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

(当然,我填写了我的api密钥并且它有效)

当我尝试将geocomplete添加到App.vue文件中的输入时:

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

我收到消息:

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

如何将此模块与Vue一起使用?

这是我的index.html文件:

<!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()
  ]
})

这是我的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>

1 个答案:

答案 0 :(得分:1)

我试图复制问题,但地理填充工作正常。

我所做的只是:

  1. 使用vue-cli创建项目。 vue init webpack my-projectnpm i
  2. 启用Google Map API。
  3. 修改index.htmlApp.vue,如下所示。
  4. <强>的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>
    

    <强>结果

    enter image description here