我在main.ts中有以下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");如何在nuxt3中添加vue3打开层插件?
发布于 2021-12-02 00:53:20
若要在Nuxt 3中自动安装Vue插件,请在<projectDir>/plugins/下创建一个.js/.ts文件(如果需要,请创建目录),其模板如下:
// plugins/my-plugin.js
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.vueApp.use(/* MyPlugin */)
})由于vue3-openlayers依赖于window__,所以只能安装客户端,所以使用.client.js扩展。
要加载vue3-openlayers客户端,plugin文件如下所示:
// plugins/vue3-openlayers.client.js
import { defineNuxtPlugin } from '#app'
import OpenLayers from 'vue3-openlayers'
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.vueApp.use(OpenLayers)
})使用以下<projectDir>/components/MyMap.vue创建文档
// components/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>我们只想在客户机上呈现MyMap,因为插件只是客户端的,所以使用作为包装:
// app.vue
<template>
<ClientOnly>
<MyMap />
<template #fallback> Loading map... </template>
</ClientOnly>
</template>https://stackoverflow.com/questions/70189545
复制相似问题