【问题标题】:load data in table vueJS在表 vueJS 中加载数据
【发布时间】:2020-10-21 09:21:36
【问题描述】:

我有一个探针可以将数据库中的数据加载到我在 vueJS 创建的表中。我在 app.js 中创建了组件表和脚本,但在视图中我可以看到这个错误:

    [Vue warn]: Property or method "datosUsuario" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <Formularioactualizacion> at resources/js/components/datosUsuarios.vue

是 v-for 的问题,因为它没有从脚本 vue 中检测到我的数组。我检查了我的数组是否为空,但不是,他有数据。我也有一个新的加载数据用户和其他加载视图的路径,一切都很好,但我不能将数据加载到表中。我附上了我的实际代码。

app.js

 require('./bootstrap');

window.Vue = require('vue');


Vue.component('usuarios-component', require('./components/usuariosComponent.vue').default);
Vue.component('formularioactualizacion', require('./components/datosUsuarios.vue').default);


// inicio de VUE

const app = new Vue({
    el: '#contenedorVue',
    created: function(){
        this.cargar();
    },
    data: {
        datosUsuario: [],
    },
    methods: {
        cargar: function(){
            let url = '/getDatosPersonales';
            axios.get(url).then((response) => {
                this.datosUsuario = response.data;
            
            }).catch((error) => console.error(error));             

        },
        enviar(){
            let url = '/actualizarDatos';
            axios.post(url, {
                id: this.id,
                nombreUsuario: this.nombreUsuario,
                email: this.email,
                password: this.password,
                direccion: this.direccion
            }).then(function(response){
                this.arrayTasks = response.data;
            }).catch(function(error){
                console.log(error);
            })
        }
    }
});

组件

    <template>
    <div class="tabla-usuarios">
        <table class="table table-hover table-striped">
            <thead>
                <th>ID</th>
                <th>NOMBRE</th>
                <th>EMAIL</th>
                <th>DIRECCIÓN</th>
                <th>CONTRASEÑA</th>
            </thead>
            <tbody>
                <tr v-for="usuario in datosUsuario" :key="usuario.id">
                    <td>{{ usuario.id }}</td>
                    <td>{{ usuario.nombre }}</td>
                    <td>{{ usuario.email }}</td>
                    <td>{{ usuario.direccion }}</td>
                    <td>{{ usuario.password }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script>
export default {
    data() {
        return {
            datosUsuario: [],
        };
    },
    created: function () {
        this.cargar();
    },
    methods: {
        cargar: function () {
            let url = "/getDatosPersonales";
            axios
                .get(url)
                .then((response) => {
                    this.datosUsuario = response.data;
                    console.log(this.datosUsuario);
                })
                .catch((error) => console.error(error));
        },
    },
};
</script>

我的问题出在这个 v-for 的组件中......我是 vueJS 的新手,我正在这个框架中启动托盘。

非常感谢您的帮助

编辑

    [Vue warn]: The "data" option should be a function that returns a per-instance value in component definitions.
warn @ app.js:38441
./node_modules/vue/dist/vue.common.dev.js.strats.data @ app.js:39068
mergeField @ app.js:39372
mergeOptions @ app.js:39367
Vue.extend @ app.js:42959
Vue.<computed> @ app.js:43037
./resources/js/app.js @ app.js:49878
__webpack_require__ @ app.js:20
0 @ app.js:50103
__webpack_require__ @ app.js:20
(anonymous) @ app.js:84
(anonymous) @ app.js:87
app.js:38441 [Vue warn]: Property or method "datosUsuario" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <Formularioactualizacion> at resources/js/components/datosUsuarios.vue
       <Root>

【问题讨论】:

    标签: javascript php laravel vue.js


    【解决方案1】:

    您的组件正在该组件内寻找 datosUsuario 变量,这就是您收到该错误以解决此问题的原因

    组件

      <template>
        <div class="tabla-usuarios">
            <table class="table table-hover table-striped">
                <thead>
                    <th>ID</th>
                    <th>NOMBRE</th>
                    <th>EMAIL</th>
                    <th>DIRECCIÓN</th>
                    <th>CONTRASEÑA</th>
                </thead>
                <tbody>
                    <tr v-for="usuario in datosUsuario" :key="usuario.id">
                        <td>{{ usuario.id }}</td>
                        <td>{{ usuario.nombre }}</td>
                        <td>{{ usuario.email }}</td>
                        <td>{{ usuario.direccion }}</td>
                        <td>{{ usuario.password }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </template>
    
    <script>
    export default {
        data() {
            return {
                datosUsuario: [],
            };
        },
        created: function () {
            this.cargar();
        },
        methods: {
            cargar: function () {
                let url = "/getDatosPersonales";
                axios
                    .get(url)
                    .then((response) => {
                        this.datosUsuario = response.data;
                    })
                    .catch((error) => console.error(error));
            },
            enviar() {
                let url = "/actualizarDatos";
                axios
                    .post(url, {
                        id: this.id,
                        nombreUsuario: this.nombreUsuario,
                        email: this.email,
                        password: this.password,
                        direccion: this.direccion,
                    })
                    .then(function (response) {
                        this.arrayTasks = response.data;
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
        },
    };
    </script>
    

    并删除函数形式app.js

    【讨论】:

    • 感谢您的时间和回复,但我在控制台中有此消息和您的代码。我为新消息编辑了我的问题
    • 感谢您的回复。现在组件已创建,但所有数据都是 @{usuario.id} @{usuario.nombre} @{usuario.email} @{usuario.direccion} @{usuario.password} 并重复
    • 你的代码有错误我现在修复了这些检查
    • 我根据您的回复编辑了我的问题。我可以在函数中看到一个错误,是函数 enviar。这个功能应该被删除,因为功能不应该在这里,这个功能是当我点击其他按钮时。但现在应该被删除。对不起我的错误。在控制台中,我遇到了下一个问题 [Vue 警告]:渲染错误:“TypeError:无法读取 null 的属性 'id'”,在 ---> at resources/js/components/datosUsuarios.vue
    • 基于组件的移动方法不要放在一个组件中你做了这么多错误所以尝试阅读文档vuejs.org/v2/guide
    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 2021-02-23
    • 2018-03-25
    • 1970-01-01
    • 2020-04-08
    • 2017-08-27
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多