8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Nuxt.js 2.0でvue2-google-mapsを動かす

Posted at

Nuxt.js 2.0でvue2-google-mapsを動かそうとしていろいろとハマったのでメモ。

普通にYarnでインストール

yarn add vue2-google-maps

宗教上NPMしか使えない人は好きにしてください。

~/plugins/vue2-google-maps.jsに下記を書く

~/plugins/vue2-google-maps.js
import Vue from 'vue';
import * as VueGoogleMaps from '~/node_modules/vue2-google-maps/src/main';

Vue.use(VueGoogleMaps, {
  load: {
    key: process.env.GOOGLE_MAPS_API_KEY,
    libraries: 'places',
  },
});

process.env.GOOGLE_MAPS_API_KEYは適宜置き換えてください。
あとドキュメントどおりにVueGoogleMapsをimportすると死にます。

nuxt.config.jsのWebpack設定を置き換える

nuxt.config.js
    extend(config, ctx) {
      ...
      config.externals = config.externals || [];
      if (!ctx.isClient) {
        config.externals.splice(0, 0, function(context, request, callback) {
          if (/^vue2-google-maps($|\/)/.test(request)) {
            callback(null, false);
          } else {
            callback();
          }
        });
      }
    }
config.externals = config.externals || [];

を入れないでドキュメントどおりに書いちゃう学生まで許されるお偉いくんは死にます。

nuxt.config.js
vendor: ['vue2-google-maps']

nuxt.config.js
plugins: [{ src: '~/plugins/vue2-google-maps.js', ssr: false }]

も忘れずに。
ネコに作業を妨害されてるので今日はここまで。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?