The Vue 3 Composition API - The property retrieved by setup() "is accessed during rendering but is not defined in the instance."

I've been experimenting with Vue 3's Composition API by creating a small in-app message console, but I'm having trouble pinpointing the error in my code. When rendering this component, the state is being accessed during render (in the loop), even though I believe I defined it correctly in the object returned by the setup() function:

<template>
  <div>
    <p class="message" v-for="message in state.messages" v-bind:key="message.id">{{message.text}}</p>
  </div>
</template>

<script lang="ts" >
import { Options, Vue } from 'vue-class-component';
import { consoleStore } from "@/store/ConsoleStore"

@Options({})
export default class Console extends Vue {
  setup() {
      console.log("setup entered")
      consoleStore.record("This is a test.")
      let state = consoleStore.getState()
      return {
        state
      }
  }
}
</script>

In addition, the line

console.log("setup entered")
doesn't seem to output anything to the developer console, almost as if the setup block is never entered. For the consoleStore, I have implemented a simple Vue 3 store class based on the concept outlined in this article, where it should be a readonly(reactive(stuff)):

import { reactive, readonly } from 'vue';

export abstract class AbstractStore<T extends Object> {
    protected state: T;

    constructor() {
        let data = this.data();
        this.setup(data);
        this.state = reactive(data) as T;
    }

    protected abstract data(): T

    protected setup(data: T): void { }

    public getState(): T {
        return readonly(this.state) as T
    }
}

Answer №1

One crucial step you seem to overlook is importing the setup method within the class component:

import { Options, Vue, setup } from 'vue-class-component';
import { consoleStore } from "@/store/ConsoleStore";

function useState () {
 console.log("Entering setup function")
      consoleStore.record("This is just a test.")
      let state = consoleStore.getState()
      return {
        state
      }
}
@Options({})
export default class Console extends Vue {
 state = setup(() => useState()) 
}
</script>

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

Why does updating state lead to a re-rendering loop while setting state does not encounter the same issue?

Essentially, I have developed a REST Api that retrieves a list of components (sections) from a CMS along with their associated data. Each section in the UI is assigned a number to indicate its position, but certain sections are excluded from counting, such ...

Error: Laravel Pusher Authorization Failed

My events are not working properly on my laravel 7.x setup with pusher and laravel-echo. I have double-checked everything and even set the broadcast driver to pusher in the .env file, but still getting a 401 error. If anyone has any insights or solutions, ...

I'm curious if there is a method in Vue.js that allows for creating a separator while utilizing v-for, much like the way `Array.join()` or FreeMarker's `<#sep>` functions operate

My goal is to create a list of <span> elements separated by commas using vue.js and v-for. Is there an easy way to achieve this in vue.js? In JavaScript, I would typically use users.join(", "). In FreeMarker templates, you can do some very interes ...

How do I create a generic function in TypeScript that adjusts its behavior based on the input argument?

Is there a way to create a generic function that can accept generic arguments like Data<string, number>? Take a look at this code snippet: interface Data<T,R> { a:T; c:R; } function foo( data: Data<string, number> ){ return t ...

Encountering a CORS error while attempting to initiate a Github API call on my Next App

I'm currently developing a Next.js React app with TypeScript and I am wondering if I need a server to make requests to the GitHub API. In my next.config.mjs file, as shown below, the only task my app needs is to fetch content from a file in a public r ...

Vue-Router fails to function when a route is activated

Check out the following routes: /form/1 /form/2 /form/3 While on the / address, clicking on /form/1 causes VueRouter to load my component inside router-view. However, when I'm already on /form/1, clicking on /form/2 does nothing! ...

Retrieve information from a JSON file within a Vue.js application rather than entering data manually

I am venturing into the world of Vue.js for the first time. I have created an app that currently relies on manually added data within the script. Now, I am looking to enhance it by fetching data from a JSON file, but I'm unsure about how to proceed wi ...

The Vue conditional event handling with the v-on directive and the prevent modifier

Is there a way to utilize the .prevent modifier of v-on dynamically? <a href="#" class="modal-overlay" aria-label="Close" v-on="overlayClickClosesModal ? { click: () => closeModal() } : {}" /> I'm attempting to prevent the URL from ...

What is the best approach to integrate a JQuery-powered widget into a Vue.js module for seamless use?

I have a group of colleagues who have started developing a complex web application using Vue.js. They are interested in incorporating some custom widgets I created with JQuery in the past, as re-creating them would be time-consuming and challenging. While ...

Incorporating TypeScript basics into the if statement post compiling

As I delve into the Angular2 Quickstart, I stumbled upon a peculiar issue within app.component.js after compiling app.component.ts using tsc (version 1.8.2): if (d = decorators[i]) I am unable to pinpoint where I may have gone wrong in configuring the qu ...

What values are typically used in the "types" field of a package.json file?

As a newcomer in the realms of JS/TS, I am delving into creating an NPM package using TypeScript for educational purposes. To prepare the artifacts for registry upload, it's necessary to compile the TS files into JS files using the tsc command. Here i ...

When running jest unit tests, an error is thrown stating that includes() and toLowerCase are not functions

MyComponent.js contains both toLowerCase and includes methods on the props. However, when attempting to perform unit testing on MyComponent, I encounter an issue where the functions toLowerCase() and includes() are not recognized as valid. Within MyCompon ...

The select 2 control feature appears to be missing from the Vue.js implementation

https://i.stack.imgur.com/QnTam.pngI am facing a challenge with my Vue.js setup where I need to bind a select2 using a directive instead of a template. Due to constraints, I am required to use an asp:DropDownList, resulting in the select boxes being displa ...

Can type information be incorporated during compilation?

Consider the code snippet below: function addProperties(keys: String[]): Object { // For illustration purposes, this is a specific return return { firstProperty: "first_value", secondProperty: "second_value" }; } export defaul ...

Error message: The database query function is encountering an issue where the property 'relation.referencedTable' is undefined and cannot be accessed

Currently, I am working with two schemas named products.ts and category.ts. The relationship between these files is defined as one-to-many. In the products.ts file: import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core"; import ...

Unable to utilize "util.inspect" within client-side code. Issue with Sass

Can anyone provide insight on this issue? I started encountering it after performing an npm upgrade. It seems to be linked to a known problem described here: https://github.com/vitejs/vite/issues/9238 "nuxt": "^3.0.0-rc.10", "nod ...

The functionality of the Vert.x event bus client is limited in Angular 7 when not used within a constructor

I have been attempting to integrate the vertx-eventbus-client.js 3.8.3 into my Angular web project with some success. Initially, the following code worked perfectly: declare const EventBus: any; @Injectable({ providedIn: 'root' }) export cl ...

Is there a way to establish a pre-defined key in a mat-table?

I am fetching data from an API and I need to display it in a key-value format on a mat table. The keys are predefined and not provided by the API. The data retrieved from the API is shown here: image1 I want to display it on a mat table like this: mat ta ...

Encountered the error message "Router.js file throwing a TypeError: next is not a function" while implementing navigation guards in my Vue 3 project

I'm puzzled as to why 'i' doesn't recognize the next function, even though I followed a similar video that implemented it without any errors. import Dashboard from "./Pages/Dashboard.vue"; import Customers from "./Pages/Customers.vue"; ...

Tips for efficiently loading data into a vuex module only when it is required and addressing issues with async/await functionality

Is there a method to load all the data for a Vuex store once and only load it when necessary? I believe there is, but I am having trouble implementing it. I'm not sure if it's due to my misunderstanding of Vuex or Async/Await in Javascript promi ...