Vue.js - 事件不会触发子组件在 table 中显示请求的数据
Vue.js - Event does not trigger child component to show requested data in table
我正在使用 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>
程序编译成功,使table没有数据,为了模仿API请求,我想在"Click me"点击时填充数据。不幸的是,点击它没有任何反应。
我错过了什么?
这看起来像是范围问题。 this.eventFromApp 在 App.vue 中不存在。将 HelloWorld.vue 中的所有内容移动到 App.vue 中,它应该可以工作。现在,您在 App.vue 中的方法无法与 JD-Table 组件通信。
如果您想将按钮分离到 JD-Table 组件,那么您需要让点击通过道具将数据传递给 HelloWorld.vue 组件,然后再将其发送到 JD- Table.
我正在使用 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>
程序编译成功,使table没有数据,为了模仿API请求,我想在"Click me"点击时填充数据。不幸的是,点击它没有任何反应。
我错过了什么?
这看起来像是范围问题。 this.eventFromApp 在 App.vue 中不存在。将 HelloWorld.vue 中的所有内容移动到 App.vue 中,它应该可以工作。现在,您在 App.vue 中的方法无法与 JD-Table 组件通信。
如果您想将按钮分离到 JD-Table 组件,那么您需要让点击通过道具将数据传递给 HelloWorld.vue 组件,然后再将其发送到 JD- Table.