尝试将 geocomplete 添加到 Vue js webpack 项目
Trying to add geocomplete to a Vue js webpack project
我之前已经将 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>
我尝试重现该问题,但 geocomplete 运行良好。
我所做的只是:
- 使用 vue-cli 创建项目。
vue init webpack my-project
和 npm i
.
- 启用 Google 地图 API。
- 编辑
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>
结果
我之前已经将 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>
我尝试重现该问题,但 geocomplete 运行良好。
我所做的只是:
- 使用 vue-cli 创建项目。
vue init webpack my-project
和npm i
. - 启用 Google 地图 API。
- 编辑
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>
结果