作用域插槽
2020-09-02 本文已影响0人
流泪手心_521
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.pagination {
margin: 30px 0 10px 0;
}
.pagination span {
margin-right: 5px;
padding: 10px 20px;
border: 1px solid #000;
}
.pagination span.current {
background: rgba(0,0,255, .5);
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="user of showUsers" :key="user.id">
{{user.id}} - {{user.username}}
</li>
</ul>
<hr>
<kkb-pagination :page.sync="userPage" :page-size="userPageSize" :total="users.length">
<template v-slot:title="data">
<p>总数据:{{users.length}},页码:{{data.page}} / {{data.pages}}</p>
</template>
<template>
<p>开课吧</p>
</template>
</kkb-pagination>
</div>
<script src="./js/vue.js"></script>
<script>
Vue.component('kkb-pagination', {
props: {
total: {
type: Number,
default: 0
},
pageSize: {
type: Number,
default: 2
},
page: {
type: Number,
default: 1
},
message: {
type: String,
default: ''
}
},
data() {
return {
pages: Math.ceil( this.total / this.pageSize )
}
},
template: `
<div class="pagination">
<slot name="title" :pages="pages" :page="page"></slot>
<span
v-for="p of pages" :key="p"
:class="{current: p === page}"
@click="changePage(p)"
>{{p}}</span>
<slot></slot>
</div>
`,
methods: {
changePage(p) {
this.$emit('update:page', p);
}
}
});
new Vue({
el: '#app',
data: {
users: [
{id: 1, username: 'zMouse', gender: '男'},
{id: 2, username: 'mt', gender: '男'},
{id: 3, username: 'haizi', gender: '男'},
{id: 4, username: 'liwei', gender: '男'},
{id: 5, username: 'zhangsan', gender: '男'},
{id: 6, username: 'lisi', gender: '男'},
{id: 7, username: 'wangwu', gender: '男'}
],
userPage: 1,
userPageSize: 2
},
computed: {
showUsers() {
let start = (this.userPage - 1) * this.userPageSize;
let end = start + this.userPageSize;
return this.users.filter( (user, index) => index >= start && index < end );
}
}
})
</script>
</body>
</html>