亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁 web前端 Vue.js Vue組件實戰(zhàn):分頁組件開發(fā)

Vue組件實戰(zhàn):分頁組件開發(fā)

Nov 24, 2023 am 08:56 AM
vue 組件 分頁

Vue組件實戰(zhàn):分頁組件開發(fā)

Vue元件實戰(zhàn):分頁元件開發(fā)

介紹

在網路應用程式中,分頁功能是不可或缺的一個元件。一個好的分頁元件應該展示簡潔明了,功能豐富,而且易於整合和使用。

在本文中,我們將介紹如何使用Vue.js框架來開發(fā)一個高度可自訂化的分頁元件。我們將透過程式碼範例來詳細說明如何使用Vue元件開發(fā)。

技術堆疊

  • Vue.js 2.x
  • JavaScript (ES6)
  • HTML5與CSS3

#開發(fā)環(huán)境

  • Node.js v8.9.3
  • npm v5.5.1
  • Vue.js v2.5.2

#分頁元件需求

  • 透過props接收總頁面數(total)和目前頁數(current)屬性
  • 可以設定顯示的最大頁碼數(maxShown)
  • 可以配置按鈕顯示的文字(prevText和nextText) 和按鈕樣式
  • 點擊頁碼可以切換到對應的頁面
  • 目前頁碼高亮顯示
  • 目前頁面沒有前一頁時,忽略上一頁按鈕的點擊事件
  • 當前頁面沒有後一頁時,忽略下一頁按鈕的點擊事件

設計思路和程式碼實作

根據需求,我們將分頁元件拆分為多個小元件來實現(xiàn)。我們需要建立以下3個小元件:

  1. Pagination.vue

#主頁元件,負責分頁資料和邏輯的處理。向子元件傳遞分頁訊息和回應子元件的事件。

  1. Button.vue

此元件為按鈕元件,用於建立分頁按鈕。

  1. Page.vue

此元件用於建立單一頁面區(qū)塊,包含頁標號和狀態(tài)。頁面區(qū)塊可以是目前頁面或非目前頁面。

接下來,讓我們使用程式碼來實作以上3個元件。

  1. Pagination.vue
<template>
  <div class="pagination-container">
    <button-prev :current="current" @onPrev="prev"></button-prev>
    <page v-for="page in pages"
      :key="page"
      :page="page"
      :is-selected="page === current"
      @on-page-selected="selectPage"></page>
    <button-next :current="current" :total="total" @onNext="next"></button-next>
  </div>
</template>
<script>
import ButtonPrev from './ButtonPrev.vue';
import ButtonNext from './ButtonNext.vue';
import Page from './Page.vue';

export default {
  components: { ButtonPrev, ButtonNext, Page },
  props: {
    total: {
      type: Number,
      default: 10
    },
    current: {
      type: Number,
      default: 1
    },
    maxShown: {
      type: Number,
      default: 5
    },
    prevText: {
      type: String,
      default: '上一頁'
    },
    nextText: {
      type: String,
      default: '下一頁'
    }
  },
  computed: {
    pages () {
      const start = Math.max(1, this.current - Math.floor(this.maxShown / 2));
      const end = Math.min(this.total, start + this.maxShown - 1);
      return Array.from({ length: end - start + 1 }, (v, k) => start + k);
    }
  },
  methods: {
    selectPage (page) {
      if (this.current === page) return;
      this.current = page;
      this.$emit('onPageChanged', page);
    },
    prev () {
      if (this.current > 1) {
        this.selectPage(this.current - 1);
      }
    },
    next () {
      if (this.current < this.total) {
        this.selectPage(this.current + 1);
      }
    }
  }
}
</script>

在上面的程式碼中,我們首先import了ButtonPrev、ButtonNext和Page元件。接著,用props方式取得了total, current, maxShown, prevText和nextText屬性,並定義了計算屬性pages,根據當前頁碼(current)和最大頁碼數(maxShown)得到一個包含頁碼數的數組,以在組件中呈現(xiàn)。

我們也定義了selectPage方法,在該方法中,如果頁碼(page)與目前頁碼(current)相同,則傳回或不做任何事情。否則,將新頁碼發(fā)出給父元件。

prev()和next()方法用於處理上一頁和下一頁事件,並防止event被回應。

  1. ButtonPrev.vue
<template>
    <button
      class="btn-previous"
      :disabled="current === 1"
      @click="onPrev()">
      {{ prevText }}
    </button>
</template>

<script>
export default {
  props: {
    prevText: {
      type: String,
      default: '上一頁'
    },
    current: {
      type: Number,
      default: 1
    }
  },
  methods: {
    onPrev () {
      this.$emit('onPrev');
    }
  }
}
</script>

<style scoped>
.btn-previous {
  border: none;
  color: #333;
  display: inline-block;
  font-size: 16px;
  padding: 6px 12px;
  margin-right: 5px;
  background-color:#fff;
  cursor: pointer;
  border-radius: 2px;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
}
.btn-previous:disabled {
  color: #ccc;
  cursor: default;
}
</style>

在上述程式碼中,我們首先透過props取得了目前頁碼(current)和上一頁按鈕的文字(prevText)屬性。在模版中,使用類別綁定(disabled)控制按鈕使用狀態(tài)。定義了一個onPrev方法,該方法觸發(fā)父組件的onPrev事件。

  1. ButtonNext.vue
<template>
    <button
      class="btn-next"
      :disabled="current === total"
      @click="onNext()">
      {{ nextText }}
    </button>
</template>

<script>
export default {
  props: {
    total: {
      type: Number,
      default: 10
    },
    nextText: {
      type: String,
      default: '下一頁'
    },
    current: {
      type: Number,
      default: 1
    }
  },
  methods: {
    onNext () {
      this.$emit('onNext');
    }
  }
}
</script>

<style scoped>
.btn-next {
  border: none;
  color: #333;
  display: inline-block;
  font-size: 16px;
  padding: 6px 12px;
  margin-left: 5px;
  background-color: #fff;
  cursor: pointer;
  border-radius: 2px;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
}
.btn-next:disabled {
  color: #ccc;
  cursor: default;
}
</style>

在上述程式碼中,我們將ButtonPrev.vue的程式碼複製了一份,稍微改了一下文字和判斷條件。

  1. Page.vue
<template>
  <button :class="{ current: isSelected }" class="btn-page" @click="onPageSelected(page)">
    {{ page }}
  </button>
</template>

<script>
export default {
  props: {
    page: {
      type: Number,
      required: true
    },
    isSelected: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    onPageSelected () {
      this.$emit('onPageSelected', this.page);
    }
  }
}
</script>

<style scoped>
.btn-page {
  border: none;
  color: #333;
  display: inline-block;
  font-size: 16px;
  padding: 6px 12px;
  margin-left: 5px;
  background-color: #fff;
  cursor: pointer;
  border-radius: 2px;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
}
.btn-page.current {
  background-color: #0078d7;
  color: #fff;
}
</style>

在上述程式碼中,我們透過props取得了該頁碼的值(page)和按鈕的isSelected屬性。在模板中,使用類別綁定("current")高亮顯示選取的頁面。

我們也定義了一個onPageSelected方法,該方法會觸發(fā)父元件的onPageSelected事件。

最後,這些元件可以在任何Vue.js應用程式中的template中使用,如下所示:

<template>
  <div>
    <pagination
      :total="total"
      :current="current"
      :maxShown="maxShown"
      :prevText="prevText"
      :nextText="nextText"
      @onPageChanged="onPageChanged"></pagination>
    <ul>
      <li v-for="(item, index) in items" :key="index">{{ item.name }}</li>
    </ul>
  </div>
</template>

<script>
import Pagination from './Pagination.vue';

export default {
  components: {
    Pagination
  },
  data () {
    return {
      current: 1,
      maxShown: 10,
      prevText: '上一頁',
      nextText: '下一頁',
      total: 10,
      pageSize: 10,
      items: [{ name: 'Item 1' }, { name: 'Item 2' }, { name: 'Item 3' }]
    }
  },
  methods: {
    onPageChanged (page) {
      console.log('Page changed to: ', page);
      // 當前頁面數據請求
    }
  }
}
</script>

上述程式碼中,我們引入了Pagination元件,並將其作為template中的父組件。我們也將total, current和maxShown綁定到元件,以便取得到它們的值。在onPageChanged方法中,我們可以處理頁面變更事件,並根據目前頁碼請求對應的資料。

以上是Vue組件實戰(zhàn):分頁組件開發(fā)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
怎樣開發(fā)一個完整的PythonWeb應用程序? 怎樣開發(fā)一個完整的PythonWeb應用程序? May 23, 2025 pm 10:39 PM

要開發(fā)一個完整的PythonWeb應用程序,應遵循以下步驟:1.選擇合適的框架,如Django或Flask。 2.集成數據庫,使用ORM如SQLAlchemy。 3.設計前端,使用Vue或React。 4.進行測試,使用pytest或unittest。 5.部署應用,使用Docker和平臺如Heroku或AWS。通過這些步驟,可以構建出功能強大且高效的Web應用。

Laravel   Vue.js 開發(fā)單頁面應用(SPA)教程 Laravel Vue.js 開發(fā)單頁面應用(SPA)教程 May 15, 2025 pm 09:54 PM

使用Laravel和Vue.js可以構建單頁面應用(SPA)。 1)在Laravel中定義API路由和控制器,處理數據邏輯。 2)在Vue.js中創(chuàng)建組件化前端,實現(xiàn)用戶界面和數據交互。 3)配置CORS和使用axios進行數據交互。 4)利用VueRouter實現(xiàn)路由管理,提升用戶體驗。

前端路由(Vue Router、React Router)的工作原理及配置方法? 前端路由(Vue Router、React Router)的工作原理及配置方法? May 20, 2025 pm 07:18 PM

前端路由系統(tǒng)的核心是將URL映射到組件,VueRouter和ReactRouter通過監(jiān)聽URL變化並加載相應組件實現(xiàn)無刷新頁面切換。配置方法包括:1.嵌套路由,允許在父組件中嵌套子組件;2.動態(tài)路由,根據URL參數加載不同組件;3.路由守衛(wèi),在路由切換前後執(zhí)行邏輯如權限檢查。

Vue的反應性轉換(實驗,然後被刪除)的意義是什麼? Vue的反應性轉換(實驗,然後被刪除)的意義是什麼? Jun 20, 2025 am 01:01 AM

ReactivitytransforminVue3aimedtosimplifyhandlingreactivedatabyautomaticallytrackingandmanagingreactivitywithoutrequiringmanualref()or.valueusage.Itsoughttoreduceboilerplateandimprovecodereadabilitybytreatingvariableslikeletandconstasautomaticallyreac

Vue.js 與 React 在組件化開發(fā)中的核心差異是什麼? Vue.js 與 React 在組件化開發(fā)中的核心差異是什麼? May 21, 2025 pm 08:39 PM

Vue.js和React在組件化開發(fā)中的核心差異在於:1)Vue.js使用模板語法和選項式API,而React使用JSX和函數式組件;2)Vue.js採用響應式系統(tǒng),React則使用不可變數據和虛擬DOM;3)Vue.js提供多個生命週期鉤子,React則更多使用useEffect鉤子。

如何在VUE應用程序中實施國際化(I18N)和本地化(L10N)? 如何在VUE應用程序中實施國際化(I18N)和本地化(L10N)? Jun 20, 2025 am 01:00 AM

國際化和傾斜度invueAppsareprimandermedusingthevuei18nplugin.1.installvue-i18nvianpmoryarn.2.createlo calejsonfiles(例如,en.json,es.json)fortranslationMessages.3.setupthei18ninstanceinmain.jswithlocaleconfigurationandmessagefil

Vue 響應式原理及在數組更新時不觸發(fā)視圖更新的解決方案? Vue 響應式原理及在數組更新時不觸發(fā)視圖更新的解決方案? May 20, 2025 pm 06:54 PM

Vue.js處理數組更新時,視圖未更新是因為Object.defineProperty無法直接監(jiān)聽到數組變化。解決方法包括:1.使用Vue.set方法修改數組索引;2.重新賦值整個數組;3.使用Vue重寫過的變異方法操作數組。

使用VUE中的V-For指令使用關鍵屬性(:key)的好處??是什麼? 使用VUE中的V-For指令使用關鍵屬性(:key)的好處??是什麼? Jun 08, 2025 am 12:14 AM

Usingthe:keyattributewithv-forinVueisessentialforperformanceandcorrectbehavior.First,ithelpsVuetrackeachelementefficientlybyenablingthevirtualDOMdiffingalgorithmtoidentifyandupdateonlywhat’snecessary.Second,itpreservescomponentstateinsideloops,ensuri

See all articles