溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實現(xiàn)Vuejs頁面的區(qū)域化與組件封裝

發(fā)布時間:2021-08-07 09:10:51 來源:億速云 閱讀:108 作者:小新 欄目:web開發(fā)

這篇文章主要介紹如何實現(xiàn)Vuejs頁面的區(qū)域化與組件封裝,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

組件的好處

當(dāng)我用vue寫頁面的時候,大量的數(shù)據(jù)頁面渲染,引入組件簡化主頁面的代碼量,當(dāng)代碼區(qū)域塊代碼差不多相同時,組件封裝會更加簡化代碼。組件是Vue.js最強大的功能之一。

組件可以擴展HTML元素,封裝可重用的代碼。在較高層面上,組件是自定義的元素,Vue.js的編譯器為它添加特殊功能。在有些情況下,組件也可以是原生HTML元素的形式,以is特性擴展。

我用一個讀書軟件的圖書列表例子:

圖書展示頁 大家可以想想用vue如何實現(xiàn)這個頁面的前端頁面實現(xiàn),再來實現(xiàn)邏輯功能;

圖片顯示的 '推薦圖書' 和 '最新圖書' 的列表展示是一樣的,開始可以用重復(fù)的代碼把先寫好的 '推薦圖書' 的代碼復(fù)制一份就可以輕輕松松實現(xiàn) '最新圖書' 頁面

如何實現(xiàn)Vuejs頁面的區(qū)域化與組件封裝

如果其他頁面也需要這個展示,或我想代碼更加簡潔一點,那么來組件如何封裝就派上場啦

簡要頁面:圖書列表展示頁 - 圖書列表組件

|- book.vue // 圖書展示頁面
 |-- BookList.vue // 圖書列列表組件

基礎(chǔ)部分相信使用過vue的伙計都知道如何使用,我直接上代碼:

創(chuàng)建一個組件 - 注冊組件 - 使用組件

// 引入組件
import BookList from '../../components/bookList/BookList.vue';

// 注冊組件
components:{
 BookList,
},

// 使用組件
<book-list></book-list>

vue2.0 規(guī)定引入組件建議使用駝峰命名,使用時用 - 分開,vue才更好識別

之前沒封封裝組件的代碼就不上傳了,直接上代碼:

圖書列表頁 - book.vue

|- book.vue - html 頁面
 <template> 
  <div>
  <h3>歡迎來到波波圖書館!</h3>
     
  <!-- 推薦讀書 -->
  <section class="box recommend-book">
   <!-- 大家注意 :books 是BookList.vue組件里圖書對象數(shù)組 heading 是傳給組件的標(biāo)題 -->
   <book-list :books="recommendArray" heading="推薦圖書"></book-list>
  </section>

  <!-- 最新圖書 -->
  <section class="box update-book">
   <!-- 大家注意 :books 是BookList.vue組件里圖書對象數(shù)組 heading 是傳給組件的標(biāo)題 -->
   <book-list :books="updateBookArray" heading="最新圖書"></book-list>
  </section>

  </div>
 </template>

我是模擬數(shù)據(jù),開發(fā)過程中是用api接口拿數(shù)據(jù)的,其實都一樣,代碼有點多,原理都一樣,大家看一下也可以了解一下json的知識

|- book.vue - js 
<script>
 import BookList from '../../components/bookList/BookList.vue';
 export default({
  data(){
    return {

    // 推薦圖書
    recommendArray:[
     {
      id:1,
      img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG',
      book_name:'Vuejs-1',
      book_author:'liangfengbo',
     },

     {
      id:2,
      img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG',
      book_name:'Vuejs-2',
      book_author:'liangfengbo',

     },

     {
      id:3,
      img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG',
      book_name:'Vuejs-3',
      book_author:'liangfengbo',

     },
    ],

    // 最新圖書
    updateBookArray:[
     {
      id:5,
      img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG',
      book_name:'Vuejs-5',
      book_author:'liangfengbo',

     },

     {
      id:6,
      img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG',
      book_name:'Vuejs-6',
      book_author:'liangfengbo',

     },
     {
      id:7,
      img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG',
      book_name:'Vuejs-7',
      book_author:'liangfengbo',

     },
    ],
   }
  },

  // 引入組件
  components:{
   BookList,
  },

  methods : {
     
  },
 })
</script>
|- book.vue - css
<style>
 *{
  margin: 0;
  padding: 0;
 }
 li{
  list-style:none;
 }
  .box{
  height: auto;
  border-bottom: 1px solid #efefef;
  margin: 10px 0;
  padding: 5px 0;
 }
</style>

組件 - BookList.vue

|- 組件 - BookList.vue - html
<template>
 <div>
  <!-- 頭部 -->
  <!--這個是頁面?zhèn)鱽淼臉?biāo)題 -->
  <h4 class="heading">{{heading}}</h4>
  <!-- 列表 -->
  <article class="book-list">
   <!-- 遍歷圖書數(shù)據(jù) -->
   <li v-for="book in books">

    <router-link :to="{ name:'BookDetail',params:{ id: book.id }}">
     ![](book.img_url) <!-- 圖書圖片 -->
     {{book.book_name}} <!-- 圖書名字 -->
    </router-link>

   </li>   
   </article>
 </div>
</template>

|- 組件 - BookList.vue - html

<script>
 export default({
  // props 數(shù)據(jù)傳遞的意思
  props:[
   'heading',//標(biāo)題
   'books',//圖書對象數(shù)組
  ],
  data(){
   return {
  
   }
  },
  methods : {
     
  },
 })
</script>

|- 組件 - BookList.vue - css

<style scoped>
  /*圖書列表*/
 .book-list {
  width:100%;
  height:128px;
  display: flex;
  justify-content: space-around;
 }
 .heading {
  border-left: 4px solid #333;
  margin: 10px 0;
  padding-left: 4px;
 }
 .book-list li {
  width:80px;
  height: 100%;
  flex:1;
  margin:0 10px;

 }

 .book-list li img{
  height: 100px;
  width: 100%;
 }
 .book-list li a{
  text-align: center;
  font-size: 12px;
  text-decoration: none;
  display: inline-block;
  width: 100%;
 }
</style>

全部的代碼就在這里啦,大家可以細(xì)心發(fā)現(xiàn),組件封裝,其實就向我們之前JavaScript函數(shù)封裝一樣,傳遞參數(shù),接收參數(shù),渲染數(shù)據(jù),重復(fù)利用,大家可以直接復(fù)制代碼運行看一下,注釋有解釋啦。

小干貨

父組件 調(diào)用 子組件 方法為 :

在子組件上寫上名字 如:

<start-set-timeout seconds=60 ref="contTimer"></start-set-timeout>

調(diào)用方法:this.$refs.contTimer.countTime(60)

但是

因為有數(shù)據(jù)的延遲 經(jīng)常會出現(xiàn)調(diào)用子組件的事件出現(xiàn)undefined的事情:

TypeError: Cannot read property 'countTime' of undefined

解決方法是

// 調(diào)用時加一個定時器
setTimeout(() => {
 this.$refs.contTimer.countTime(60)
}, 20)

以上是“如何實現(xiàn)Vuejs頁面的區(qū)域化與組件封裝”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI