我正在考虑升级到v3,对于看到内联模板已被删除感到失望。因此,我正在尝试转换为使用范围限定的广告位。
我已经定义了以下列表组件:
<template>
<slot :items="filteredItems" :page="page" :totalPages="totalPages" :onPageChanged="onPageChanged"></slot>
</template>
<script>
export default {
props: {
items: {
type: Array
},
initialPage: {
type: Number,
default: 1,
},
pageSize: {
type: Number,
default: 10
}
},
beforeCreate() {
this.page = this.initialPage;
},
computed: {
filteredItems() {
return this.items.slice((this.page - 1) * this.pageSize, this.page * this.pageSize);
},
totalPages() {
return Math.ceil(this.items.length / this.pageSize);
}
},
methods: {
onPageChanged(page) {
console.log('Page changed!!!');
this.page = page;
}
}
};
</script>
这样的称呼:
<list :items="[ { foo: 'A' }, { foo: 'B' }, { foo: 'C' } ]" :page-size="2" #="{ items, page, totalPages, onPageChanged }">
<ul class="list-group">
<li class="list-group-item" v-for="item in items">{{ item.foo }}</li>
</ul>
<pager :page="page" :total-pages="totalPages" @pageChanged="onPageChanged"></pager>
</list>
这是传呼机组件:
<template>
<ul class="pagination">
<li class="page-item" v-if="hasPreviousPage"><a href="#" @click="changePage(1)" class="page-link">«</a></li>
<li class="page-item" v-if="hasPreviousPage"><a href="#" @click="changePage(page - 1)" class="page-link">‹</a></li>
<li v-for="page in pages" :class="['page-item', { active: page.isActive }]"><a href="#" @click="changePage(page.name)" class="page-link">{{ page.name }}</a></li>
<li class="page-item disabled" v-if="page < totalPages - 2"><span class="page-link"> ... </span></li>
<li class="page-item" v-if="page < totalPages - 2"><a href="#" @click="changePage(totalPages)" class="page-link">{{ totalPages }}</a></li>
<li class="page-item" v-if="hasNextPage"><a href="#" @click="changePage(page + 1)" class="page-link">›</a></li>
<li class="page-item" v-if="hasNextPage"><a href="#" @click="changePage(totalPages)" class="page-link">»</a></li>
</ul>
</template>
<script>
export default {
props: {
page: {
type: Number,
required: true
},
totalPages: {
type: Number,
required: true
}
},
computed: {
hasPreviousPage() {
return this.page > 1;
},
hasNextPage() {
return this.page < this.totalPages;
},
pages() {
const range = [];
for (let i = this.page <= 2 ? 1 : this.page - 2; i <= (this.page >= this.totalPages - 2 ? this.totalPages : this.page + 2); i++) {
range.push({
name: i,
isActive: this.page == i
});
}
return range;
}
},
methods: {
changePage(page, e = event) {
e.preventDefault();
// Trigger the page changed event.
this.$emit('pageChanged', page);
}
}
};
</script>
但是,每当我尝试更改页面时,都会调用changePage
方法,该方法会发出pageChanged
事件,但不会在列表组件中调用onPageChanged
方法。 / p>
如果有人可以告诉我我做错了什么,我将不胜感激。谢谢
答案 0 :(得分:1)
事件名称应以kebab-case
格式编写,如下所示:
this.$emit('page-changed', page);
并像@page-changed="onPageChanged