Why is the Vue Yandex Maps package failing to function in Vue 3 when utilizing the defineCustomElement feature?

I am currently working on 2 different projects in Vue 3 that involve utilizing vue yandex maps:

First project:

You can view a demo of the first project where Vue Yandex Maps is being used by clicking here. In this project, the package is registered as follows:

Code snippet from main.js where vue-yandex-maps components are registered:

const { createApp } = require('vue');
import App from './App.vue';
import ymapPlugin from 'vue-yandex-maps/dist/vue-yandex-maps.esm.js';

const app = createApp(App);

app.config.isCustomElement = (tag) => tag.startsWith('y'); // <= This line does not work.
app.use(ymapPlugin);
app.mount('#app');

Code snippet from MapComponent.vue where the vue-yandex-maps package is utilized:

<template>
  <yandex-map :coords="coords">
    <ymap-marker
      marker-id="123"
      :coords="coords"
      :marker-events="['click']"
    ></ymap-marker>
  </yandex-map>
</template>

<script>
export default {
  name: 'MapComponent',
  setup() {
    return {
      coords: [54, 39],
    };
  },
};
</script>

Code snippet from App.vue where the component MapComponent is used:

<template>
  <div id="app">
    <MapComponent />
  </div>
</template>

<script>
import MapComponent from './components/MapComponent.vue';

export default {
  name: 'App',
  components: {
    MapComponent,
  },
};
</script>

Second project:

In the second project, a new feature called defineCustomElement from Vue version 3.2 was used and an error message was encountered when incorporating the vue-yandex-maps package:

Uncaught TypeError: Cannot read properties of null (reading 'offsetWidth')

Code snippet from main.js where vue-yandex-maps components are registered using the new feature:

import { defineCustomElement } from './defineCustomElementWithStyles'
import App from './App.ce.vue'
import store from './store'
import router from './router'
import ymapPlugin from 'vue-yandex-maps/dist/vue-yandex-maps.esm.js'

customElements.define(
  'app-root',
  defineCustomElement(App, {
    plugins: [store, router, ymapPlugin],
  })
)

Code snippet from defineCustomElementWithStyles.js:

import { defineCustomElement as VueDefineCustomElement, h, createApp, getCurrentInstance } from 'vue'

// Code for defining custom elements with styles

// More code here....

Question:

Can you help identify the source of the error in the second project where I am using vue-yandex-maps with defineCustomElement?

Answer №1

vue-yandex-maps displays a map container featuring a unique randomly generated ID that is provided to the ymaps.Map constructor. This ID is later used to locate the corresponding element in the document. However, due to the fact that the map container resides within the Shadow DOM of the app-root custom element, it remains hidden from standard document queries. When attempting to retrieve the size of the container using document.querySelector(), a null reference is returned, resulting in the error you encountered.

To address this issue, you may need to make modifications to vue-yandex-maps yourself or alternatively raise a GitHub issue to propose a change where the map container element (from the custom element's Shadow DOM) could be directly passed instead of an ID. It appears that ymaps.Map already supports accepting either an element or a string ID, indicating that no significant alterations would be needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Styling elements using the nth-child selector in JavaScript

I am trying to apply a CSS class based on the combined height of the 2nd and 3rd child elements. For example, if the height of the 2nd child plus the height of the 3rd child equals a certain value, I want to add a specific class. Here is my JavaScript cod ...

The delete function does not appear to be functioning properly for JavaScript objects within a Node.js

I'm currently working with an event object retrieved from MongoDB using Mongoose plug-in. After performing some operations on this object, I need to save it in another collection with the same structure. In order to do this, I have to remove all prop ...

When incorporating Routes into App, it is important to note that React does not accept functions as valid children

I am currently working on wrapping Routes using a Layout component to organize all content within a bootstrap 12 column grid. However, I am facing an issue where my text inside Route components is not being wrapped and I receive a warning stating that func ...

Vue and Vuex: input value remains unchanged when there's no change in the store

<template> <input @input="formatInputValue" type="text" :value="formattedInput" /> </template> <script type="text/javascript"> import {formatPhoneNumber} from '~/utils/string'; export default { co ...

"When utilizing a jQuery AJAX request to communicate with a Node.js server, the functionality only seems to

I'm diving into the world of AJAX and feeling a bit lost. On my webpage, I use an AJAX GET request to fetch comments on a post. However, there's a glitch where the request only succeeds after refreshing the page. I even tried disabling the cache, ...

Utilizing global enumerations within VueJS

Is there a way to effectively utilize global enums in Vue or declare them differently? My current setup is as follows: Within my types/auth.d.ts: export {}; declare global { enum MyEnum { some = "some", body = "body", o ...

The Next.js API endpoint is struggling to process cross-domain POST requests

Dealing with a POST request in my NextJS application has been challenging. This particular post request is originating from a different domain. To address this issue, I included a receptor.ts file in the /pages/api directory: import { NextApiRequest, Next ...

Tips for sending eventData parameters to .on() by utilizing the .trigger() function from jQuery?

My event handler on the <select> element looks like this: select.on("change", null, { tbody: $("#tbody"), colspan: 5, onLoad: onLoadHandler, }, onSelectChange); Inside the onSelectChange function, I access these parameters via the event ...

Troubleshooting Vue Computed Property Doesn't Refresh

I am experiencing an issue with a computed function that I want to use. Every time I attempt to remove the forward slashes and 'SYG' at the end of the string "99/KRFS/010572//SYG" pasted into a v-model input, I receive the error message "Computed ...

Error: Unable to access the applicant's ID as it is undefined

I'm currently facing an issue with passing parameters from server.js to humanresources.js in a login request. Although the params are successfully printed out in server.js, they appear as "undefined" once passed on to the function in human resources.j ...

Drawing a line beneath the mouse's center using JavaScript

Struggling with my JavaScript drawing tool, particularly the draw() function. The line is consistently off-center below the mouse cursor. How do I fix this issue? My aim is to have the line always follow the center of the mouse as it moves. Could someone c ...

Error message: The property or method "Name" is not defined on this object, but it is being referenced during the rendering process

Currently facing a challenge in implementing the modal closure functionality within the Vue.js template. Take a look at the code snippet: var modalInstance = new Vue({ el: "#modalInstance", data: { displayModal: false } }); Vue.compo ...

Encountered an issue while attempting to write to SQLite, error code 6 is

I'm working on a project that involves writing to a SQLite Database from a cross-platform app built with AngularJS, Monaca, and Onsen UI. Within my app, there's a view where users input their username and password. I store these details in a Ser ...

ESLint version 8.0.0 encountered an error while attempting to fetch the '@typescript-eslint' plugin

Hey there, I'm in need of some assistance. I encountered an error while trying to build a project. Uh-oh! Something didn't go as planned! :( ESLint: 8.0.0 TypeError: Failed to load plugin '@typescript-eslint' specified in ' ...

What is the process for 'injecting' data into a Vue component?

Trying to understand components better and struggling to adapt the official vuejs.org guide examples from new Vue({...}) to the module structure in a newly created Vue app using the CLI. If I have something like this: const products = { 'bat': ...

What is the process for displaying node_modules directories in a json/javascript document?

Exploring ways to showcase the dependencies within my d3.js tree, I am curious if it's possible to dynamically list the dependencies' names in a JSON file or directly within the javascript file. I'm puzzled by how JavaScript can access folde ...

Is there a way to cancel an ongoing AJAX request?

My website will soon have a series of jQuery AJAX calls, and I need to create a function to abort these calls. Can anyone provide guidance on how to achieve this? I have already reviewed this particular link, but it did not work for me as expected. ...

Attempting to create a three-dimensional illusion using Three.js

I'm striving to create a mesmerizing effect similar to this masterpiece: Here's my progress so far: https://codepen.io/routsou/pen/ZEGWJgR?editors=0010 /*-------------------- Setup --------------------*/ console.clear(); const canvas = document. ...

Display various messages when submitting a form based on Ajax technology

I have been working on a script that utilizes AJAX to process form submissions. While I can successfully submit the form using AJAX and display a success message, I am looking to customize the messages based on the background data processing of the form. ...

Deactivate and circumvent Angular routing outside of the specified route URL

I am looking for a way to disable and bypass Angular routing when I have static pages or PHP-rendered views in my hybrid PHP and AngularJS app. Instead of being redirected by Angular's routing, I want to perform a full page reload to the routed link o ...