最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Can I declare a function inside a reactive object in Vue 3? - Stack Overflow

matteradmin4PV0评论

The following reactive object has a variable and also a function declared inside of it:

const state = reactive({
    names: [] as string[],

    add(name: string): void {
        this.names.push(name);
    },
});

So that I can call it directly:

state.add('foo');

The official docs say nothing about having functions inside reactive.

Is there any problem with this approach?

The following reactive object has a variable and also a function declared inside of it:

const state = reactive({
    names: [] as string[],

    add(name: string): void {
        this.names.push(name);
    },
});

So that I can call it directly:

state.add('foo');

The official docs say nothing about having functions inside reactive.

Is there any problem with this approach?

Share edited 20 hours ago rodrigocfd asked 23 hours ago rodrigocfdrodrigocfd 8,1278 gold badges46 silver badges79 bronze badges 1
  • 1 It's perfectly normal. There would be a problem if "this" were bound to wrong context – Estus Flask Commented 23 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

Yes! You can define a function inside a reactive object, Vue reactive is a proxy that tracks property access and updates, so whenever you update the names array, the change will be accessible.

Be careful with this keyword, with normal functions add() {...} dynamically bind this to the reactive object, but arrow functions add: () => {...} do not have their own this, for more details about this see https://stackoverflow/a/134149/29157031

Here is an example:

<script setup lang="ts">
import { reactive } from 'vue'

const state = reactive({
    names: [] as string[],
    add(name: string): void {
        this.names.push(name);
    },
    add_2: (name: string): void => {
        // this.names.push(name); // ERROR: Cannot read properties of undefined (reading 'names')
        state.names.push(name);
    },
});

setTimeout(()=>{
  state.add('1');
  state.add_2('2');
}, 2000)

</script>
<template>
  <button @click="()=>state.add('John')">Add Name 1</button>
  <button @click="()=>state.add_2('John')">Add Name 2</button>
  <p>Names: {{ state.names }}</p>
  <p>Count: {{ state.names.length }}</p>
</template>

see this vue playground to test the code

Post a comment

comment list (0)

  1. No comments so far