javascript - How to add vue3-openlayers plugin to nuxt - Stack Overflow

I have the following main.ts file in Vue3:import { createApp } from "vue"import App from &q

I have the following main.ts file in Vue3:

import { createApp } from "vue"
import App from "./App.vue";

//How to do this in nuxt3?
import OpenLayersMap from "vue3-openlayers";
import "vue3-openlayers/dist/vue3-openlayers.css";

const app = createApp(App);

//How to do this in nuxt3?
app.use(OpenLayersMap);

app.mount("#app");

How can I add the vue3-openlayers plugin to nuxt3?

I have the following main.ts file in Vue3:

import { createApp } from "vue"
import App from "./App.vue";

//How to do this in nuxt3?
import OpenLayersMap from "vue3-openlayers";
import "vue3-openlayers/dist/vue3-openlayers.css";

const app = createApp(App);

//How to do this in nuxt3?
app.use(OpenLayersMap);

app.mount("#app");

How can I add the vue3-openlayers plugin to nuxt3?

Share Improve this question edited Dec 1, 2021 at 18:58 Philip F. asked Dec 1, 2021 at 18:36 Philip F.Philip F. 1,2374 gold badges19 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

To auto-install a Vue plugin in Nuxt 3, create a .js/.ts file under <projectDir>/plugins/ (create the directory if needed) with the following boilerplate:

// plugins/my-plugin.js
import { defineNuxtPlugin } from '#app'

export default defineNuxtPlugin(nuxtApp => {
  nuxtApp.vueApp.use(/* MyPlugin */)
})

Since vue3-openlayers depends on window, the plugin can only be installed client side, so use the .client.js extension.

To load vue3-openlayers client side, the plugin file would look like this:

// plugins/vue3-openlayers.client.js
import { defineNuxtPlugin } from '#app'
import OpenLayers from 'vue3-openlayers'

export default defineNuxtPlugin(nuxtApp => {
  nuxtApp.vueApp.use(OpenLayers)
})

Create <projectDir>/ponents/MyMap.vue with the following example content from the vue3-openlayers docs:

// ponents/MyMap.vue
<script setup>
import { ref } from 'vue'
const center = ref([40, 40])
const projection = ref('EPSG:4326')
const zoom = ref(8)
const rotation = ref(0)
</script>

<template>
  <ol-map :loadTilesWhileAnimating="true" :loadTilesWhileInteracting="true" style="height:400px">
    <ol-view :center="center" :rotation="rotation" :zoom="zoom"
    :projection="projection" />
    <ol-tile-layer>
        <ol-source-osm />
    </ol-tile-layer>
  </ol-map>
</template>

<style scoped>
@import 'vue3-openlayers/dist/vue3-openlayers.css';
</style>

We only want to render MyMap on the client because the plugin is only client-side, so use the <ClientOnly> ponent as a wrapper:

// app.vue
<template>
  <ClientOnly>
    <MyMap />
    <template #fallback> Loading map... </template>
  </ClientOnly>
</template>

demo

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744170542a4561514.html

相关推荐

  • javascript - How to add vue3-openlayers plugin to nuxt - Stack Overflow

    I have the following main.ts file in Vue3:import { createApp } from "vue"import App from &q

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信