Hey there, I'm having some trouble getting the mounted function to work properly in vue.js

My goal is to automatically fetch all products when the specified URL is loaded:

'/Admin/ProductsAdmin'

Unfortunately, the mounted function is not functioning properly as it fails to load anything upon URL loading. Below is the main.js code snippet:

var app = new Vue({
    el: "#app",
    data: {
        loading: false,
        objectIndex: 0,
        productModel: {
            id: 0,
            name: "Product Name",
            description: "Product Description",
            value: 1.99
        },
        products: []
    },
    mounted() {
        this.getProducts();
        console.log("mounted");
    },
    methods:
    {
        getProduct(id) {
            this.loading = true;
            axios.get('/Admin/ProductsAdmin' + id)
                .then(res => {
                    console.log(res);
                    this.products = res.data;

                })
                .catch(err => { console.log(err); })
                .then(() => { this.loading = false; });

        },
        getProducts() {
           this.loading = true;
            axios.get('/Admin/ProductsAdmin')
                .then(res => {
                    console.log(res);
                    this.products = res.data;

                })
                .catch(err => { console.log(err); })
                .then(() => { this.loading = false; });

        },
        createProduct() {
            this.loading = true;
            axios.post('/Admin/products', this.productModel)
                .then(res => {
                    console.log(res.data);
                    this.products.push(res.data);
                })
                .catch(err => { console.log(err); })
                .then(() => { this.loading = false; });

        },
       
        updateProduct() {
    this.loading = true;
            axios.put('/Admin/products', this.productModel, {
                headers: { 'Content-Type': 'application/json' }
            })
                .catch(err => { console.log(err); })
                .then(() => {
                    this.loading = false;
                });
        },
        deleteProduct(id,index) {
            this.loading = true;
            axios.delete('/Admin/products/' + id)
                .then(res => {
                    console.log(res);
                    this.products.splice(index, 1);

                })
                .catch(err => { console.log(err); })
                .then(() => { this.loading = false; });

        },
        editProduct(product, index) {
            this.objectIndex = index;
            this.producModel = {
                id: product.id,
                name: product.name,
                description: product.description,
                value: product.value,
            };
                
        }

    },
    computed: {

    }

});

Upon refreshing or loading the specific page, the mounted function fails to execute. I am seeking assistance from anyone who can help resolve this issue.

Answer №1

Utilize the created method.

Rather than using

mounted() {
        this.getProducts();
        console.log("mounted");
    },

Opt for one of the Vue lifecycle hooks such as created().

created() {
        this.getProducts();
        console.log("created");
    },

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

How can you effectively blend Vue/Angular with other JavaScript resources to enhance your development process?

My curiosity lies in understanding how front-end javascript libraries such as Vue and Angular can seamlessly integrate with other libraries and assets. For instance, if I were to procure a website template already equipped with additional javascript, is it ...

Leveraging Apostrophe CMS for building a decoupled single page application

I am currently developing a single page application inspired by the design of My goal is to separate the content management system from the presentation layer (most likely using Vue or Angular) and fetch data from a JSON API. The client initially prefers ...