Issue with Vue property's sub-object not inheriting methods from prototype

Incorporating Vue into my project to showcase an intricate hexagonal grid has been quite challenging. Utilizing the Honeycomb library for handling the grid data in memory has proven to be both beneficial and complex. The library introduces a specialized Grid object that not only stores the essential data but also offers a wide range of manipulation methods, drawing inspiration from the functionalities of a typical Array.

Initially, I attempted to pass the grid object as a property to my Vue component:

@Component
export default class TileMap extends Vue {
@Prop() private grid!: Grid;

Unfortunately, I encountered an issue where all the methods associated with the Grid object seemed to get lost within my Vue component.

  • Upon creating the Grid object, the methods were present in the prototype.
  • However, when utilizing the Grid property within my component, the methods were mysteriously absent from the prototype.

Is this behavior to be expected? Should I steer clear of utilizing properties for objects with extensive properties and methods?

Answer №1

Regrettably

The requirement is that the object must be plain: native objects like browser API objects and prototype properties will not be considered. It is advisable to keep data as pure data - observing objects with their own stateful behavior is not recommended.

source

I haven't come across any effective workaround for this situation.

Should we store things in the window object?

Perhaps wrapping grid logic in a service with internal state?

Or encapsulate grid in a function and pass it to a child component as a prop?

The latter option surprisingly works well.

import { defineGrid, Grid, GridFactory } from 'honeycomb-grid'
import Test from './Test.vue'
import { Vue, Component, Prop, Provide, ProvideReactive } from "vue-property-decorator";

@Component({
    components : { Test }
})
export default class App extends Vue {
    @Provide() private gridFunc: () => Grid = null;

    mounted() {
        const grid = defineGrid().rectangle({ width: 4, height: 4 });
        this.gridFunc = () => grid;
    }
}
import { defineGrid, Grid, GridFactory } from 'honeycomb-grid'
import { Vue, Component, ProvideReactive, Prop, Watch } from "vue-property-decorator";

@Component({})
export default class Test extends Vue {
    @Prop() private gridFunc!: () => Grid<any>;
    
    @Watch('gridFunc')
    onGridChange(val: string, oldVal: string) {
        console.log(this.gridFunc());
    }
}

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

In the setup function, the composition API calculates the return value of the computed property before it is

I am currently working on editing a post using the state manager Vuex in Vue3 with Composition API. Below is the code I have implemented: <template> <div class="container py-5"> <h3 class="mb-5 border-top-0 border-start- ...

Ways to steer clear of utilizing subscriptions and BehaviorSubject.value through a declarative method within rxjs

As I refactor my Angular application, my goal is to eliminate all subscriptions and rely solely on the async pipe provided by Angular for a declarative approach instead of an imperative one. I encounter difficulties implementing a declarative approach whe ...

Obtain the specific generic type that is employed to broaden the scope of a

I am working on a class that involves generics: abstract class Base<P extends SomeType = SomeType> { // ... } In addition, there is a subclass that inherits from it: class A extends Base<SomeTypeA> { // ... } I'm trying to figure out ...

Setting character limits within textareas based on CSS classes in a dynamic manner

I'm looking to develop a JavaScript function that can set text limits for multiple textareas at once using a class, allowing for flexibility in case specific textareas need to be exempt. However, I'm facing issues with my debuggers - Visual Studi ...

Navigating through nested objects using Rxjs

How to Extract Specific Attribute Values from Nested Objects Array using RxJS const obj = { name: 'campus', buildings: [ { name: 'building', floors: [ { name: 'floo ...

Utilizing twig variables with Vue.js within Symfony 4

I have integrated a component called "vue-cookie-law" into my Symfony application. In the default Twig template, I call it like this: <div id="CookieLaw" data-locale="{{ app.request.locale }}"></div> In my Vue template, I have the following c ...

What is the method for HTML inline handlers to retrieve the global window object and the variables contained within it?

During my coding test, I encountered an interesting scenario. I had a function called write and used a button with an inline onclick handler to trigger the write() function. function write(text) { alert(text) } <button onclick='write("Some tex ...

What is the best way to move between components within the same parent class using UI router in Angular 6?

Explore the Angular UI-Router Visualizer design.component.ts import { Component, OnInit, ChangeDetectorRef, EventEmitter, Output, Input } from '@angular/core'; import { AppService } from '@app/shared/app.service'; import { Schema } fr ...

Tips for resolving the issue of invalid functions as a child component in React

When I call a function that returns HTML code, everything works fine until I try to pass a parameter in. At that point, I receive an error saying "Functions are not valid as a React child." The issue is that I need to access the props from this function. T ...

Sorting customization within a complex nested array structure

Sorting a nested array can sometimes be tricky. Consider a JSON structure like the one shown below: var orders = [{ 'orderId': 1, 'sales': [{ 'salesNumbers': 3 }] }, { 'orderId': 2, ...

In Chrome, the computed style of background-position is returned as 0% 0%

Let's say I have an element and I am interested in finding out its background-position: To achieve this, I use the following code snippet: window.getComputedStyle(element).getPropertyValue('background-position') If the value of background ...

Is there a way to selectively update specific keys within existing objects in Mongodb without affecting the rest of the

Here is the scenario I am dealing with: Within my Angular 8 application, I am responsible for creating and managing invoices. The structure of an invoice object looks like this: { "_id": { "$oid": "5ea9ad58f65d8d49841362bd" }, "details": [ { ...

403 Malicious Path Middleware Error in Express.js

Encountering an error when sending a post request to my server, but only on the production server - whereas the staging server is functioning properly. Both servers are hosted on AWS Ubuntu instances. Investigating the stack trace, it appears that the err ...

Guide on implementing hover effects in React components

Within my component SecondTest, I have defined an object called useStyle which sets the background color to red. Is it feasible to add a hover effect to this object? const useStyle = { backgroundColor: "red", }; function SecondTest() { return < ...

Show information in a React Native element | Firebase

Just starting out with react native and struggling to display data in a component? You're not alone! I'm having trouble too and would love some guidance on how to destructure the data for display. Any tips? import React,{useState,useEffect} from ...

Why does my event dispatch only run once upon form submission in Svelte JS?

My code successfully fetches data and puts it in a card when new data is added to the input. However, the issue arises when more than one data entry is made - although the data gets added to the database, it does not reflect in the data list. Can anyone he ...

Encountering an issue when trying to download a PDF from an Angular 6 frontend using a Spring Boot API - receiving an error related to

When I directly call the Spring Boot API in the browser, it successfully creates and downloads a PDF report. However, when I try to make the same GET request from Angular 6, I encounter the following error: Here is the code snippet for the Spring Boot (Ja ...

Learn the process of generating an array from a computed property array field in Vue

I have a computed property in my VueX store that fetches data like this: computed: {...mapGetters(["allCategories"])}, Each category in this.allCategories has an ID and a name: { "id": "123", "name": "Foo" } I want to extract the names from allCategor ...

Express.js router defining a module issue

I have encountered a problem while working on my Express.js project. The 'slug' variable that I defined in app.js is not being recognized in the controllers within the router. Is there a way to define these variables in a central location, as I w ...

What are the best practices for securely storing SSL certificates and public/private keys?

I possess keys that appear like this. MIID0DCCArigAwIBAgIBATANBgkqhkiG9w0BAQUFADB/MQswCQYDVQQGEwJGUjET MBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UEBwwFUGFyaXMxDTALBgNVBAoMBERp bWkxDTALBgNVBAsMBE5TQlUxEDAOBgNVBAMMB0RpbWkgQ0ExGzAZBgkqhkiG9w0B CQEWDGRpbWlAZGltaS5mcjA ...