首页  >  问答  >  正文

无法跳转到另一页的问题在使用laravel-Vue-pagination时的解决方法

<p>在一个表格中,数据被显示出来,但是当我点击由Laravel Vue分页提供的页码时,它显示给我相同的页码1。嗯,主要问题是页码没有传递到函数(Composable API)中。</p> <p><strong>模板表格</strong></p> <pre class="brush:php;toolbar:false;"><table class="table table-striped table-bordered"> <thead> <th>SN</th> <th>账户类型</th> <th>姓名</th> <th>店铺名称</th> <th>地址</th> <th>联系电话</th> <th>电子邮件</th> </thead> <tbody> <tr v-for="(account, i) in accounts.data" :key="account.id"> <td>{{ ++i }}</td> <td>{{ account.account_type }}</td> <td>{{ account.name }}</td> <td>{{ (account.shop_name)?account.shop_name: 'EMPTY'}}</td> <td>{{ account.address }}</td> <td>{{ account.contact_number }}</td> <td>{{ account.email }}</td> </tr> </tbody> </table> <Pagination :data="accounts" @pagination-change-page="getAccounts" /> </template></pre> <p>在:data中传递了来自Composable API的数据,在@pagination-change-page中也传递了函数(Composable)。<strong>脚本标签</strong></p> <pre class="brush:php;toolbar:false;">import LaravelVuePagination from 'laravel-vue-pagination'; export default { components: { 'Pagination': LaravelVuePagination }, setup() { } const paginates = ref(10); const { accounts, loading, getAccounts } = accountDetails();//Composables API onMounted(() => { getAccounts({paginate:paginates}); }); return { accounts, loading,getAccounts }; }, };</pre> </p><p> <p><strong>可组合 API</strong> 在这个函数中,我接收了两个参数,这里是Pagination(@pagination-change-page)应该抛出页码的地方!</p> <pre class="brush:php;toolbar:false;">import { ref } from '@vue/reactivity'; import axios from 'axios'; const accountDetails = () => { const accounts = ref({}); const loading = ref(true); const accountError = ref({}); const getAccounts = async ({page=1,paginate}) => { await axios.get('api/account/getAccounts?page='+page+'paginate='+paginate, { headers: { Authorization: `Bearer ${localStorage.getItem('token')}`, Accept: 'application/json', } }).then((res) => { accounts.value = res.data; loading.value = false; console.log(accounts.value); }).catch((resErr) => { loading.value = false; accountError.value = resErr; }) } return { accounts, loading, accountError, getAccounts } } export default accountDetails;</pre></p>
P粉598140294P粉598140294442 天前619

全部回复(1)我来回复

  • P粉164942791

    P粉1649427912023-08-29 00:48:39

    成功使用Composition API Vue3进行工作
    只需从getAccounts中删除paginate参数,并且不要将paginates传递给该函数。

    更新的代码 </>

    脚本

    import LaravelVuePagination from 'laravel-vue-pagination';
    
    export default {
        components: {
            'Pagination': LaravelVuePagination
        },
      setup() {
        }
       
        const { accounts, loading, getAccounts } = accountDetails();//Composables API    
        onMounted(() => {
          getAccounts();
        });
    
        return { accounts, loading,getAccounts };
      },
    };

    可组合的API

    import { ref } from '@vue/reactivity';
    import axios from 'axios';
    
    const accountDetails = () => {
        const accounts = ref({});
        const loading = ref(true);
        const accountError = ref({});
    
    
        const getAccounts = async (page=1) => {
            await axios.get('api/account/getAccounts?page='+page, {
                headers: {
                    Authorization: `Bearer ${localStorage.getItem('token')}`,
                    Accept: 'application/json',
                }
            }).then((res) => {
                accounts.value = res.data;
                loading.value = false;
                console.log(accounts.value);
            }).catch((resErr) => {
                loading.value = false;
                accountError.value = resErr;
            })
        }
        return { accounts, loading, accountError, getAccounts }
    }
    export default accountDetails;

    回复
    0
  • 取消回复