gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:21:48 25 4
gpt4 key购买 nike

我正在使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com