【发布时间】:2019-03-04 16:41:58
【问题描述】:
应用程序具有各种工具(页面)及其结构。例如酒店搜索工具(在列表的左侧窗格中,在右侧地图上),或者报告工具(全宽页面),这样的工具有很多。
我在github 上发现了一个用于处理面板的有趣组件。然后他开始做这样的事情:
//MyPanes.vue
<splitpanes class="default-theme" horizontal>
<span>
<div class="left-panel"><slot name="left"></slot></div>
</span>
<span splitpanes-min="20" splitpanes-max="50" :splitpanes-default="horizontal_splitter[1]">
<div class="right-panel"><slot name="right"></slot></div>
</span>
</splitpanes>
//HotelTool.vue
<MyPanes>
<template slot="left">
<v-btn @click.native="add">Add counter</v-btn>
</template>
<template slot="right">
<div>{{ counter }}</div>
<v-map />
</template>
</MyPanes>
..
data: {
counter: 0
},
methods: {
add() { this.counter++; }
}
..
计数器是为测试而设计的。但它不起作用。可能是因为slot的内容不知道parent的数据。
于是问题就来了。如何实现这种行为?需要放弃插槽?
当然,你可以通过event bus或者store来实现,但是我有很多工具,用这种方式传输数据不是很方便。我想在一个文件中创建仪器,在其中为左右部分编写逻辑,以便它们相互了解。所有工具也是如此。
你能分享你的想法吗?如何在单亲上下文中更改插槽之间的数据?还有其他解决方案吗?
Example tool Example tool 左侧和右侧可能有不同的组件。
完整代码:
<template>
<MyPanes>
<template v-slot:left>
<h1>Мониторинг</h1>
<v-btn @click.native="add">{{ counter }}</v-btn>
</template>
<template v-slot:right>
{{counter}}
</template>
</MyPanes>
</template>
<script>
export default {
data() {
return {
counter: 0,
}
},
methods: {
add: function () {
this.counter++;
}
}
}
</script>
【问题讨论】:
标签: javascript vue.js architecture