1

could it be that Inertia.js page components are blocking the reactivity of vue?

I have a Page component, in this component is a normal single file component.

I have a function that adds items to the ItemsManager.items object. When I'm running this function the single component below doesnt adds this items in the v-for. But when I'm reload the Page Component it works and the previously added items appear.

Here the single file component:

<template>
    <div>
        <div v-for="item in items" :key="item.$key">
            test
        </div>
    </div>
</template>

<script>
import { ItemsManager } from "./utils.js";

export default {
    name: "test-component",
    data: () => ({
        items: ItemsManager.items
    }),
};
</script>

utils.js:

export const ItemsManager = {
    items: [],
    add(item) {
        item.$key = this.items.length;
        this.items.unshift(item);
    },
};

function that adds the items (in page component):

addItem(title, options) {
    ItemsManager.add({
        name: title,
        options: options
    });
},

Thanks in advance!

1 Answer 1

0

Since you're using Vue2, you need to know that there are some caveats when adding/deleting things to Objects/Arrays. You don't show any code relevant to your actual way of adding stuff to your object, but I can still recommend that you'd check this page to understand and fix your issue.

https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. I'm using vue3 and I added the missing code in the question.
You're maybe using Vue3 but with option API syntax, so Vue2 essentially. Otherwise, please provide the correct vue3. The unshift here, looks fine. What do you vue devtools tell you once you call your add method ?
In the devtools the object stays empty. But when I'm refreshing the page component with the <breeze-nav-link /> component, the previously added items appear in the object and vue renders them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.