vuejs2 - 带有 Vue 选项卡的组件标签中的动态道具
问题描述
我有一个我自己无法解决的问题。
我想用 Laravel 和 Vue (SPA) 创建一个简单的新闻门户应用
我有一个带有许多选项卡(Vue 选项卡)的 Home / Index 组件
在每个标签中,我想根据它们的类别显示不同的新闻,比如健康、技术、游戏等
所以在我的 App.vue
<template>
<div>
<button type="button"
v-for="tab in tabs" :key="tab"
@click="changeTab(tab)"
>{{ tab }}</button>
</div>
<div>
<component :is="currentTab"></component>
</div>
</template>
<script>
import Health from '...'
import Technology from '...'
import Gaming from '...'
import ...
import ...
export default {
components: {
Health,
Technology,
Gaming,
...
},
data() {
return {
currentTab: 'Health', // Starting point, (when user visit the index)
tabs: [
'Health',
'Technology',
'Gaming',
....
],
health: [
// object, title, image, etc, source
],
technology: [
// object, title, image, etc, source
],
...
}
},
methods: {
changeTab(val) {
this.currentTab = val
}
}
}
</script>
如何使用道具(或任何其他方式传递它们)将数据从这个组件传递到健康、技术、游戏组件?
<component :is="currentTab"></component>
// Idk how to pass them within this component tag
解决方案
在不知道这些组件期望它们的道具采用什么结构的情况下,我建议采用这样的方法:
<component
:is="currentTab"
v-bind="currentProps"
/>
...
computed: {
currentProps() {
return this[this.currentTab.toLowerCase()];
}
}
推荐阅读
- python - Keras中的多元泊松损失函数
- laravel - 在 foreach 循环中遍历 laravel 的结果时,它只保存新数组元素中的最后一个元素?
- sql-server - 子查询中的动态 SQL 和 WITH 公用表表达式
- python - 如何使用 Firebase 和基于 linux 的客户端应用程序与服务器进行双向消息通信
- html - 更改站点上部分选定锚点的颜色 NO JAVASCRIPT
- java - 了解 Java 问题中的 ICAP 服务器文件传输
- laravel - Laravel - 使用 Rule::unique 验证输入
- javascript - jQuery-modal ajax child 中的 Javascript 失败
- html - 具有相同宽度的两个元素显示的宽度不同
- matlab - 是否可以抑制“输入”跳转到下一行?