【发布时间】:2020-02-06 13:47:17
【问题描述】:
我遇到了 Vue 2 的问题。我有一个名为 search.vue <product-search></product-search> 的主要组件。这个组件有一个叫做:view的属性,这个视图可以是表格、小列、中列等等。所有这些视图都是自定义组件:table_view.vue、small_columns.vue 等。在我看来,我有一个名为操作<slot name="actions"></slot> 的插槽。现在我想向主组件(product-search.vue)添加操作并将它们传递给视图组件。例如:
有人知道我是怎么做到的吗?
为了更好地阅读,我减少了代码量。
blank_page.vue
<template>
<div>
<search-product :multiple="false" :shop="shop" v-model="items" display="table" :modal="false">
<template slot="table-actions">
<div>Here some extra actions</div>
</template>
</search-product>
</div>
</template>
<script>
export default {
props: {
shop: {}
},
data() {
return {
items: []
};
}
}
</script>
search.vue
<template>
<div>
<template>
<component v-bind:is="getDisplay()" :multiple="multiple" :items="items" v-model="selectedItems"></component>
</template>
</div>
</template>
<script>
import SearchButton from './search-button.vue';
import SearchInput from './input.vue';
import DisplayTable from './displays/table.vue';
import DisplaySmallGrid from './displays/small-grid.vue';
import DisplayMediumGrid from './displays/medium-grid.vue';
import DisplayLargeGrid from './displays/large-grid.vue';
export default {
components: {
searchButton: SearchButton,
searchInput: SearchInput,
displayTable: DisplayTable,
displaySmallGrid: DisplaySmallGrid,
displayMediumGrid: DisplayMediumGrid,
displayLargeGrid: DisplayLargeGrid,
},
props: {
modal: {
default: false
},
multiple: {
default: true
},
value: {},
shop: {},
display: {
default: 'table'
}
},
watch: {
selectedItems(data) {
this.$emit('input', data);
}
},
data() {
return {
q: '',
items: [],
selectedItems: [],
modalOpen: false,
templates: {
DisplayTable
}
};
},
methods: {
getDisplay() {
return 'display' + this.display.charAt(0).toUpperCase() + this.display.slice(1);
},
toggleModal() {
this.modalOpen = !this.modalOpen;
}
}
}
</script>
table_view.vue
<template>
<div :class="panel ? 'card' : ''">
<div :class="panel ? 'card-body' : ''">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="item in items">
<td v-text="item.id"></td>
<td>
<slot name="table-actions"></slot>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<script>
export default {
props: {
items: {},
value: {},
multiple: {},
panel: {
default: true
}
},
}
</script>
【问题讨论】:
标签: javascript vue.js vuejs2