vue3-google-map与Nuxt

huangapple go评论60阅读模式
英文:

vue3-google-map with Nuxt

问题

我在一些Vue项目中已经使用过这个vue3-google-map插件,但现在我正在开发一个Nuxt项目,我也想在这里使用它。有没有办法将它添加到Nuxt中?

这是文档链接:

英文:

I've already used this vue3-google-map plugin in some Vue projects but now I'm working on a Nuxt project and I want to use this here as well. Is there any way to add it to Nuxt?

Here is the documentation:

答案1

得分: 3

根据我所找到的信息,这个库不支持在Nuxt 3中作为全局'plugin'使用。

我找到的大多数可用示例以及我们在项目中使用的方法都是这样的:

  1. 使用你喜欢的包管理器在你的Nuxt应用中安装该插件。
  2. 不要将插件添加到 ~/plugins/ 中。
  3. 在你的组件或模板中使用以下方式导入地图组件:
import { GoogleMap, Marker, etc } from 'vue3-google-map'

注意:尽管Jakub提出的方法对于许多类似的依赖项是有效的,但对于vue3-google-map,这种方法对我们不起作用。Jakub的答案是适用于 fawmi/vue-google-maps 的一种有效方法,它在 这里 稍作调整。

英文:

From what I can find, this library doesn't support use as a global 'plugin' within Nuxt 3.

Most of the working examples I could find, and the approach we're using in our project, do the following:

  1. Install the plugin within your Nuxt app using whichever package manager you use.
  2. Skip adding the plugin in ~/plugins/
  3. Import the map components within your components or templates using:

import { GoogleMap, Marker, etc } from 'vue3-google-map'

Note: The approach suggested by Jakub, whilst being valid for many such dependencies, did not work for us with vue3-google-map. Jabub's answer is a working method for fawmi/vue-google-maps which has been tweaked slightly from here

答案2

得分: 1

  1. 在Nuxt项目中,您应该能够使用vue3-google-map插件。

  2. 使用终端安装插件:

    npm install vue3-google-map
    
  3. 在您的Nuxt项目的plugins目录中创建一个名为vue-google-maps.js的新文件。

  4. vue-google-maps.js文件中,导入vue3-google-map插件并将其添加到Nuxt插件数组中:

    import Vue from 'vue';
    import VueGoogleMaps from 'vue3-google-maps';
    
    Vue.use(VueGoogleMaps, {
      load: {
        key: 'YOUR_API_KEY',
        libraries: 'places',
      },
    });
    
  5. 在您的Nuxt配置文件(nuxt.config.js)中,将vue-google-maps.js文件添加到plugins数组中:

    export default {
      // ...
      plugins: [
        // ...
        '@/plugins/vue-google-maps',
      ],
      // ...
    }
    
  6. 在您的Vue组件中,您现在可以使用vue3-google-map插件提供的<GmapMap>组件来显示Google地图。

英文:

You should be able to use the vue3-google-map plugin in a Nuxt project.

  1. Install the plugin with terminal:

    npm install vue3-google-map
    
  2. Create a new file called vue-google-maps.js in the plugins directory of your Nuxt project.

  3. In the vue-google-maps.js file, import the vue3-google-map plugin and add it to the Nuxt plugins array:

    import Vue from &#39;vue&#39;;
    import VueGoogleMaps from &#39;vue3-google-maps&#39;;
    
    Vue.use(VueGoogleMaps, {
      load: {
        key: &#39;YOUR_API_KEY&#39;,
        libraries: &#39;places&#39;,
      },
    });
    
  4. In your Nuxt configuration file (nuxt.config.js), add the vue-google-maps.js file to the plugins array:

    export default {
      // ...
      plugins: [
        // ...
        &#39;@/plugins/vue-google-maps&#39;,
      ],
      // ...
    }
    
  5. In your Vue component, you can now use the &lt;GmapMap&gt; component provided by the vue3-google-map plugin to display a Google Map.

huangapple
  • 本文由 发表于 2023年1月9日 04:20:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定