【问题标题】:Trying to add geocomplete to a Vue js webpack project尝试将 geocomplete 添加到 Vue js webpack 项目
【发布时间】:2018-04-10 08:02:33
【问题描述】:

我之前在 Vue 项目中添加了 Geocomplete,但没有使用 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>

【问题讨论】:

  • 你的项目中是否包含了 jquery?
  • @EricGuan 是的。在我包含它之前,它抱怨 $ 没有被定义,所以我知道它被包含了
  • 您是否将 jQuery 和 Geocomplete 直接作为脚本标签包含在 index.html 文件中?或者只是来自主应用程序 js 文件的 required,以便 webpack 将它们捆绑在一起?
  • @karamarimo 我将它们直接包含在 index.html 文件中。我会发布我的代码...
  • 但是根据错误信息,webpack知道jQuery是一个依赖,对吧?

标签: jquery webpack vue.js geocomplete


【解决方案1】:

我试图重现该问题,但 geocomplete 工作正常。

我所做的只是:

  1. 使用 vue-cli 创建项目。 vue init webpack my-project 和 npm i。
  2. 启用 Google 地图 API。
  3. 编辑index.html和App.vue如下。

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>

结果

【讨论】:

  • 你使用 npm 安装 geocomplete 了吗?
  • @DavidJ。不,我没有添加任何包。
  • 我将我的 App.vue 添加到问题中。我将尝试按照您的示例,看看我是否遇到任何问题。感谢您的帮助!
  • 我按照您的说明从头开始重试,它成功了,再次感谢。
  • @DavidJ。没问题。
猜你喜欢
  • 2019-01-22
  • 2017-12-21
  • 2018-04-05
  • 1970-01-01
  • 2019-03-18
  • 2015-04-05
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
相关资源
最近更新 更多