您好,我正在尝试在 vue.js 中使用挂载函数,但它不起作用

Hello, I am trying to work with mounted function in vue.js and it is not working

提问人:prajwal lama 提问时间:11/2/2023 更新时间:11/3/2023 访问量:23

问:

我正在尝试在以下url加载时自动getallproducts

'/Admin/ProductsAdmin'

但是挂载的函数不起作用,加载 URL 时它不会加载任何内容 下面的代码是我的主要.js代码:-

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.productModel = {
                id: product.id,
                name: product.name,
                description: product.description,
                value: product.value,
            };
                
        }

    },
    computed: {

    }

});

当我刷新页面或加载以下页面安装功能不起作用时,它什么也没做。请谁能帮我。

vue.js vue-component vuetify.js vue-composition-api

评论

0赞 tomy0608 11/2/2023
首先,您应该确保是否被触发console.log("mounted");

答:

0赞 Keyboard Corporation 11/3/2023 #1

使用已创建

而不是

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

使用 Vue 生命周期钩子之一,即 .created()

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