javascript - Vue.js - 事件不会触发子组件在表中显示请求的数据

标签 javascript vue.js datatable async.js

我正在使用vue-jd-table ,尝试使用预设的数据对象制作简单的应用程序(无需发出API请求)。按照官方使用指南,我成功地初始化和配置了应用程序,但无法使其加载数据。

main.js

import Vue from 'vue'
import JDTable from 'vue-jd-table';
import App from './App.vue'

import "@fortawesome/fontawesome-free/css/all.min.css";
import 'vue-jd-table/dist/jd-table.min.css';

Vue.config.productionTip = false
Vue.component( 'jdtable',JDTable );
new Vue
({
  render: h => h(App),
}).$mount( '#app' );

App.vue

<template>
  <div id="app">
    <div @click="trigger" class="trigger">Click me</div>
    <HelloWorld></HelloWorld>

  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
  name: 'app',
  components: {
    HelloWorld
  },
    methods :
    {
      trigger()
      {
        // Async call to a Vue method that returns a promise.
        let tableData =
        [
          {
              businessName : 'Burger King',
              founderName  : 'James McLamore'
          },
          {
              businessName : 'Mc Donalds',
              founderName  : 'Maurice McDonald'
          },
          {
              businessName : 'Wendies',
              founderName  : 'Dave Thomas'
          }
        ]

        this.eventFromApp =
        {
            name    : 'sendData',
            payload : tableData
        };
        this.triggerEvent();
      },
        // Triggers the currently queued JD-Table event to run.
        triggerEvent : function ()
        {
          // Trigger the event.
          this.eventFromAppTrigger = true;
          // Reset the trigger event.
          this.$nextTick( () =>
          {
            this.eventFromAppTrigger = false;
          });
        },
    }
}

</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

/components/HelloWorld.vue

<template>
    <div id="app">
        <!-- JD-TABLE REQUIRED - TEMPLATE -->
        <JDTable
            :option                 = "tableOptions"
            :loader                 = "tableLoader"
            :event-from-app         = "eventFromApp"
            :event-from-app-trigger = "eventFromAppTrigger"
            @event-from-jd-table    = "processEventFromApp( $event )"
        />

        <!-- JD-TABLE REQUIRED - EXCEL EXPORT -->
        <iframe id="excelExportArea" style="display:none"></iframe>
    </div>
</template>

<script>
    // JD-TABLE REQUIRED - COMPONENT REGISTRATION
    import "@fortawesome/fontawesome-free/css/all.min.css";
    import JDTable from 'vue-jd-table';

    export default
    {
        name : 'MyApp',

        // JD-TABLE REQUIRED - COMPONENT REGISTRATION
        components:
        {
            JDTable
        },

        // JD-TABLE REQUIRED - OPTIONS/PROPS
        data ()
        {
            return {
                tableOptions        : { 
                                  columns :
                [
                    {
                        name          : 'businessName',
                        title         : 'Business Name',
                        order         : 1,
                        type          : 'String',
                        filterable    : true,
                        enabled       : true,
                        sort          : true,
                        sortDirection : 'asc',
                        width         : 50,
                    },
                    {
                        name        : 'founderName',
                        title       : 'Founder Name',
                        order       : 2,
                        type        : 'String',
                        filterable  : true,
                        enabled     : true,
                        width       : 50,
                    }
                ]
                },
                eventFromApp        : { name : null, data : null },
                eventFromAppTrigger : false,
                tableLoader         : false,
                columns             : [ ]
            }
        }
    }
</script>

<style lang="scss">
    // JD-TABLE OPTIONAL - VARIABLE OVERRIDE

    // JD-TABLE REQUIRED - THEME
    @import "~vue-jd-table/dist/assets/jd-table.scss";
</style>

程序编译成功,生成没有数据的表格,为了模仿 API 请求,我想在单击“Click me”时用数据填充它。不幸的是,点击它没有任何作用。

我错过了什么?

最佳答案

这看起来像是一个范围问题。 App.vue 中不存在 this.eventFromApp。将 HelloWorld.vue 中的所有内容移至 App.vue 中,它应该可以工作。目前,App.vue 中的方法无法与 JD-Table 组件进行通信。

如果您想将按钮与 JD-Table 组件分开,那么您需要让单击通过 prop 将数据传递给 HelloWorld.vue 组件,然后该组件会将其发送到 JD-Table。

关于javascript - Vue.js - 事件不会触发子组件在表中显示请求的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59203402/

相关文章:

javascript - 如何避免 javascript 从不存在的元素中检索值

javascript - 从页面隐藏 div

c# - 对数据表的 LINQ 查询返回不正确的结果

php - 我如何通过 php 检查当前窗口是否为 "top"(或 "parent")?

javascript - 如何在 P5js/JavaScript 中按名称循环函数

javascript - 如何将属性发送到输入文本的值? (Vue.JS 2)

vue.js - 没有 .vue 扩展组件和 webpack,我们可以制作 vue.js 应用程序吗?

javascript - Vue-动态导入vue组件

c# - MySqlBulkLoader 使用 DataTable 而不是文件

c# - DataGrid绑定(bind)到DataTable时如何刷新DataGrid行?