欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue項(xiàng)目數(shù)據(jù)動(dòng)態(tài)過濾實(shí)踐及實(shí)現(xiàn)思路

 更新時(shí)間:2018年09月11日 14:55:50   作者:SHERlocked93  
這篇文章主要介紹了Vue項(xiàng)目數(shù)據(jù)動(dòng)態(tài)過濾實(shí)踐,,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

這個(gè)問題是在下在做一個(gè)Vue項(xiàng)目中遇到的實(shí)際場(chǎng)景,這里記錄一下我遇到問題之后的思考和最后怎么解決的(老年程序員記性不好 -。-),過程中會(huì)涉及到一些Vue源碼的概念比如 $mount 、 render watcher 等

問題是這樣的:頁面從后臺(tái)拿到的數(shù)據(jù)是由 0 、 1 之類的key,而這個(gè)key代表的value比如 0-女 、 1-男 的對(duì)應(yīng)關(guān)系是要從另外一個(gè)數(shù)據(jù)字典接口拿到的;類似于這樣的Api

{
 "SEX_TYPE": [
 { "paramValue": 0, "paramDesc": "女" },
 { "paramValue": 1, "paramDesc": "男" }
 ]
}

那么如果view拿到的是 0 ,就要從字典中找到它的描述 女 并且顯示出來;下面故事開始了

1. 思考

有人說,這不是過濾器 filter 要做的事么,直接Vue.filter不就行了,然而問題是這個(gè)filter是要等待異步的數(shù)據(jù)字典接口返回之后才能拿到,如果在 $mount 的時(shí)候這個(gè)filter沒有找到,那么就會(huì)導(dǎo)致錯(cuò)誤影響之后的渲染(白屏并報(bào)undefined錯(cuò));

我想到的解決方法有兩個(gè):

把接口變?yōu)?a rel="external nofollow" target="_blank" >同步,在 beforeCreate 或 created 鉤子中同步地獲取數(shù)據(jù)字典接口,保證在 $mount 的時(shí)候可以拿到注冊(cè)好的filter,保證時(shí)序,但是這樣會(huì)阻塞掛載,延長(zhǎng)白屏?xí)r間,因此不推介;

把filter的注冊(cè)變?yōu)楫惒剑讷@取filter之后通知 render watcher 更新自己,這樣可以利用vue自己的響應(yīng)式化更新視圖,不會(huì)阻塞渲染,因此在下初步采用了這個(gè)方法。

2. 實(shí)現(xiàn)

因?yàn)閒ilter屬于 asset_types ,關(guān)于在Vue實(shí)例中asset_types的訪問鏈有以下幾個(gè)結(jié)論;具體代碼實(shí)踐可以參考: Codepen - filter test

1.asset_types 包括 filters 、 components 、 directives ,以下所有的 asset_types 都自行替換成前面幾項(xiàng)

2.子組件中的 asset_types 訪問不到父組件中的 asset_types ,但是可以訪問到全局注冊(cè)的掛載在 $root.$options.asset_types.__proto__ 上的 asset_types ,這里對(duì)應(yīng)源碼 src/core/util/options.js

3.全局注冊(cè)方法Vue.asset_types,比如Vue.filters注冊(cè)的asset_types會(huì)掛載到根實(shí)例(其他實(shí)例的 $root )的 $options.asset_types.__proto__ 上,并被以后所有創(chuàng)建的Vue實(shí)例繼承,也就是說,以后所有創(chuàng)建的Vue實(shí)例都可以訪問到

4.組件的slot的作用域僅限于它被定義的地方,也就是它被定義的組件中,訪問不到父組件的 asset_types ,但是可以訪問到全局定義的 asset_types

5.同理,因?yàn)閙ain.js中的 new Vue() 實(shí)例是根實(shí)例,它中注冊(cè)的 asset_types 會(huì)被掛載在 $root.$options.asset_types 上而不是 $root.$options.asset_types.__proto__ 上
根據(jù)以上幾個(gè)結(jié)論,可以著手coding了~

2.1 使用根組件的filters

因此首先我考慮的是把要注冊(cè)的filter掛載到根組件上,這樣其他組件通過訪問 $root 可以拿到注冊(cè)的filter,這里的實(shí)現(xiàn):

<template>
 <div>
  {{ rootFilters( sexVal )}}
 </div>
</template>
<script type='text/javascript'>
 import Vue from 'vue'
 import { registerFilters } from 'utils/filters'
 export default {
  data() {
   return {
    sexVal: 1 // 性別
   }
  },
  methods: {
   /* 根組件上的過濾器 */
   rootFilters(val, id = 'SEX_TYPE') {
    const mth = this.$root.$options.filters[id]
    return mth && mth(val) || val
   }
  },
  created() {
   // 把根組件中的filters響應(yīng)式化
   Vue.util.defineReactive(this.$root.$options, 'filters', this.$root.$options.filters)
  },
  mounted() {
   registerFilters.call(this)
    .then(data =>
     // 這里獲取到數(shù)據(jù)字典的data
    )
  }
 }
</script>

注冊(cè)filter的js

// utils/filters
import * as Api from 'api'
/**
* 獲取并注冊(cè)過濾器
* 注冊(cè)在$root.$options.filters上不是$root.$options.filters.__proto__上
* 注意這里的this是vue實(shí)例,需要用call或apply調(diào)用
* @returns {Promise}
*/
export function registerFilters() {
 return Api.sysParams()      // 獲取數(shù)據(jù)字典的Api,返回的是promise
  .then(({ data }) => {
   Object.keys(data).forEach(T =>
    this.$set(this.$root.$options.filters, T, val => {
     const tar = data[T].find(item => item['paramValue'] === val)
     return tar['paramDesc'] || ''
    })
   )
   return data
  })
  .catch(err => console.error(err, ' in utils/filters.js'))
}

這樣把根組件上的filters變?yōu)轫憫?yīng)式化的,并且在渲染的時(shí)候因?yàn)樵?rootFilters 方法中訪問了已經(jīng)在created中被響應(yīng)式化的 $root.$options.filters ,所以當(dāng)異步獲取的數(shù)據(jù)被賦給 $root.$options.filters 的時(shí)候,會(huì)觸發(fā)這個(gè)組件render watcher的重新渲染,這時(shí)候再獲取 rootFilters 方法的時(shí)候就能取到filter了;

那這里為什么不用Vue.filter方法直接注冊(cè)呢,因?yàn)?Object.defineProperty 不能監(jiān)聽 __proto__ 上數(shù)據(jù)的變動(dòng),而全局Vue.filter是將過濾器注冊(cè)在了根組件 $root.$options.asset_types.__proto__ 上,因此其變動(dòng)不能被響應(yīng)。

這里的代碼可以進(jìn)一步完善,但是這個(gè)方法存在一定的問題,首先這里使用了 Vue.util 上不穩(wěn)定的方法,另外在使用中到處可見 this.$root.$options 這樣訪問vue實(shí)例內(nèi)部屬性的情況,不太文明,讀起來也讓人困惑。

因此在這個(gè)項(xiàng)目做完等待測(cè)試的時(shí)候我思考了一下,誰說過濾器就一定放在filters里面 -。-,也可以使用mixin來實(shí)現(xiàn)嘛

2.2 使用mixin

使用mixin要注意一點(diǎn),因?yàn)関ue中把data里所有以 _ 、 $ 開頭的變量都作為內(nèi)部保留的變量, 并不代理到當(dāng)前實(shí)例上 ,因此直接 this._xx 是無法訪問的,需要通過 this.$data._xx 來訪問。

// mixins/sysParamsMixin.js
import * as Api from 'api'
export default {
 data() {
  return {
   _filterFunc: null,    // 過濾器函數(shù)
   _sysParams: null,    // 獲取數(shù)據(jù)字典
   _sysParamsPromise: null // 獲取sysParams之后返回的Promise
  }
 },
 methods: {
  /* 注冊(cè)過濾器到_filterFunc中 */
  _getSysParamsFunc() {
   const thisPromise = this.$data._sysParamsPromise
   return thisPromise || Api.sysParams()      // 獲取數(shù)據(jù)字典的Api,返回的是promise
    .then(({ data }) => {
     this.$data._filterFunc = {}
     Object.keys(data).forEach(paramKey =>
      this.$data._filterFunc[paramKey] = val => {    // 過濾器注冊(cè)到_filterFunc中
       const tar = data[paramKey].find(item => item['paramValue'] === val)
       return tar['paramDesc'] || ''
      })
     return data
    })
    .catch(err => console.error(err, ' in src/mixins/sysParamsMixin.js'))
  },
  /* 按照鍵值獲取單個(gè)過濾器 */
  _rootFilters(val, id = 'SEX_TYPE') {
   const func = this.$data._filterFunc
   const mth = func && func[id]
   return mth && mth(val) || val
  },
  /* 獲取數(shù)據(jù)字典 */
  _getSysParams() {
   return this.$data._sysParams
  }
 },
 mounted() {
  this.$data._filterFunc ||
  (this.$data._sysParamsPromise = this._getSysParamsFunc())
 }
}

這里把 Api 的promise保存下來,如果其他地方還用到的話直接返回已經(jīng)是 resolved 狀態(tài)的promise,就不用再次去請(qǐng)求數(shù)據(jù)了。

那在我們的組件中怎么使用呢:

<template>
 <div>
  {{ _rootFilters( sexVal )}}
 </div>
</template>
<script type='text/javascript'>
 import * as Api from 'api'
 import sysParamsMixin from 'mixins/sysParamsMixin'
 export default {
  mixins: [sysParamsMixin],
  data() {
   return { sexVal: 1 }
  },
  mounted() {
   this._getSysParamsFunc()
    .then(data =>
     // 這里獲取到數(shù)據(jù)字典的data
    )
  }
 }
</script>

這里不僅注冊(cè)了過濾器,而且也暴露了數(shù)據(jù)字典,以方便某些地方的列表顯示,畢竟這是實(shí)際項(xiàng)目中常見的場(chǎng)景。

參考:

  1. Vue.js 2.5.17 源碼
  2. Vue 2.5.17 filter test

總結(jié)

以上所述是小編給大家介紹的Vue項(xiàng)目數(shù)據(jù)動(dòng)態(tài)過濾實(shí)踐,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 深入淺析nuxt.js基于ssh的vue通用框架

    深入淺析nuxt.js基于ssh的vue通用框架

    Nuxt.js 是一個(gè)基于 Vue.js 的通用應(yīng)用框架。 通過對(duì)客戶端/服務(wù)端基礎(chǔ)架構(gòu)的抽象組織, Nuxt.js 主要關(guān)注的是應(yīng)用的 UI渲染,需要的朋友可以參考下
    2019-05-05
  • vue $nextTick實(shí)現(xiàn)原理深入詳解

    vue $nextTick實(shí)現(xiàn)原理深入詳解

    這篇文章主要介紹了vue $nextTick實(shí)現(xiàn)原理深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • vue uniapp實(shí)現(xiàn)分段器效果

    vue uniapp實(shí)現(xiàn)分段器效果

    這篇文章主要為大家詳細(xì)介紹了vue uniapp實(shí)現(xiàn)分段器效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Vue?vant使用ImagePreview實(shí)現(xiàn)預(yù)覽圖片

    Vue?vant使用ImagePreview實(shí)現(xiàn)預(yù)覽圖片

    這篇文章主要介紹了Vue?vant使用ImagePreview實(shí)現(xiàn)預(yù)覽圖片,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 基于Vue2實(shí)現(xiàn)印章徽章組件

    基于Vue2實(shí)現(xiàn)印章徽章組件

    這篇文章主要介紹了如何基于vue2實(shí)現(xiàn)簡(jiǎn)單的印章徽章控件,文中通過示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的朋友們下面就跟隨小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-10-10
  • vue-router中的hash和history兩種模式的區(qū)別

    vue-router中的hash和history兩種模式的區(qū)別

    大家都知道vue-router有兩種模式,hash模式和history模式,這里來談?wù)剉ue-router中的hash和history兩種模式的區(qū)別。感興趣的朋友一起看看吧
    2018-07-07
  • 如何在 vue3 中使用高德地圖

    如何在 vue3 中使用高德地圖

    這篇文章主要介紹了如何在 vue3 中使用高德地圖,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • 解決Vue keep-alive 調(diào)用 $destory() 頁面不再被緩存的情況

    解決Vue keep-alive 調(diào)用 $destory() 頁面不再被緩存的情況

    這篇文章主要介紹了解決Vue keep-alive 調(diào)用 $destory() 頁面不再被緩存的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vue如何使用AIlabel標(biāo)注組件

    vue如何使用AIlabel標(biāo)注組件

    這篇文章主要介紹了vue如何使用AIlabel標(biāo)注組件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue3?企業(yè)級(jí)組件庫框架搭建?pnpm?monorepo實(shí)戰(zhàn)示例

    Vue3?企業(yè)級(jí)組件庫框架搭建?pnpm?monorepo實(shí)戰(zhàn)示例

    這篇文章主要為大家介紹了Vue3?企業(yè)級(jí)組件庫框架搭建?pnpm?monorepo實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11

最新評(píng)論