最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

幾個(gè)你不知道的技巧助你寫(xiě)出更優(yōu)雅的vue.js代碼

 更新時(shí)間:2018年06月11日 00:31:06   作者:Runlin  
本文參考自油管上某個(gè)國(guó)外大神的公開(kāi)演講視頻,學(xué)習(xí)了一下覺(jué)得很不錯(cuò),所以在項(xiàng)目中也使用了這些不錯(cuò)的技巧。趁周末有空,寫(xiě)個(gè)博客記錄一下

1. watch 與 computed 的巧妙結(jié)合

如上圖,一個(gè)簡(jiǎn)單的列表頁(yè)面。

你可能會(huì)這么做:

 created(){
  this.fetchData()
 },
 
 watch: {
  keyword(){
   this.fetchData()
  }
 }

如果參數(shù)比較多,比如上圖

  • 關(guān)鍵字篩選,
  • 區(qū)域篩選,
  • 設(shè)備ID篩選,
  • 分頁(yè)數(shù),
  • 每頁(yè)幾條數(shù)據(jù),

可能會(huì)是這樣:

data(){
 return {
  keyword:'',
  region:'',
  deviceId:'',
  page:1
 }
},
methods:{
 fetchData(paramrs={
  keyword:this.keyword,
  region:this.region,
  deviceId:this.deviceId,
  page:this.page,
 }){
  this.$http.get("/list",paramrs).then("do some thing")
 }
},
created(){
 this.fetchData()
},
watch: {
 keyword(data){
  this.keyword=data
  this.fetchData()
 },
 region(data){
  this.region=data
  this.fetchData()
 },
 deviceId(data){
  this.deviceId=data
  this.fetchData()
 },
 page(data){
  this.page=data
  this.fetchData()
 },
 requestParams(params){
  this.fetchData(params)
 }
}

不過(guò)這么寫(xiě),明顯有問(wèn)題,主要是watch了很多參數(shù),而且函數(shù)的處理都差不多,可以修改一下,通過(guò)methods處理

data(){
 return {
  keyword:'',
  region:'',
  deviceId:'',
  page:1
 }
},
methods:{
 paramsChange(paramsName,paramsValue){
  this[paramsName]=paramsValue
  this.fetchData()
 },
 fetchData(paramrs={
  keyword:this.keyword,
  region:this.region,
  deviceId:this.deviceId,
  page:this.page,
 }){
  this.$http.get("/list",paramrs).then("do some thing")
 }
},
created(){
 this.fetchData()
}

當(dāng)然這么寫(xiě),需要在模板里面每個(gè)參數(shù)change的地方綁定事件,并傳遞參數(shù)值,比如分頁(yè)change時(shí):

<el-pagination
 layout="total, prev, pager, next, jumper"
 :total="total"
 prev-text="上一頁(yè)"
 next-text="下一頁(yè)"
 @current-change="paramsChange('page',$event)"
 >
</el-pagination>

相比上面的各種watch,代碼明顯少了很多,但是還有一個(gè)問(wèn)題,那就是要在template的很多地方綁定change事件。

最后,當(dāng)然是使用我們重點(diǎn)推薦的computed + watch

data(){
 return {
  keyword:'',
  region:'',
  deviceId:'',
  page:1
 }
},
computed:{
 requestParams() {
  return {
   page: this.page,
   region: this.region,
   id: this.deviceId,
   keyword: this.keyword
  }
 }
},
methods:{
 fetchData(paramrs={
  keyword:this.keyword,
  region:this.region,
  deviceId:this.deviceId,
  page:this.page,
 }){
  this.$http.get("/list",paramrs).then("do some thing")
 }
},
watch: {
 requestParams: {
  handler: 'fetchData',
  immediate: true
 }
},

通過(guò)增加一個(gè)computed屬性,watch這個(gè)屬性并設(shè)置immediate為true,無(wú)需再手動(dòng)綁定事件,相比之上的方法都要簡(jiǎn)潔。當(dāng)然,缺點(diǎn)就是對(duì)性能稍微有些影響,不過(guò)問(wèn)題不大。

2. 使用mixin提取公共部分

很多列表頁(yè)其實(shí)使用的很多屬性都是一樣的,比如

  • 分頁(yè) page
  • 數(shù)量 size
  • 搜索關(guān)鍵 字keyword
  • 表格數(shù)據(jù) tableData

這些公共的部分其實(shí)可以通過(guò)mixin來(lái)提取出來(lái)

/**
 * mixin/table.js
 */
export default {
 data() {
  return {
   keyword: '',
   requestKeyword: '',
   pages: 1,
   size: 10,
   total: 0,
   tableData: []
  }
 }
}

在要用到的頁(yè)面

import mixin from '@/mixin/table'
export default {
 mixins: [mixin],
 data() {
  return {   
   selectRegion: '',
   selectDevice: '',
   deviceList: [],
  }
 }
 /* 其他代碼 */
 ...

3. 自動(dòng)注冊(cè)全局組件

正常情況下,我們需要使用一個(gè)我們自己封裝的組件時(shí),需要先引入,再注冊(cè),最后才能在template模板中使用。

<template>
 <all-region :selectRegion="selectRegion" @region-change="selectRegion=$event"/>
</template>

<script>
import AllRegion from './baseButton'

export default {
 components: {
  AllRegion,
 }
}
</script> 

當(dāng)有多個(gè)頁(yè)面需要用到這些組件時(shí),那么就需要在每個(gè)需要的頁(yè)面重復(fù)這些步驟。

為了簡(jiǎn)化這些步驟,可以考慮把這些組件作為全局組件來(lái)使用,這樣每個(gè)頁(yè)面需要時(shí),就可以直接使用了。

不過(guò)還有一個(gè)問(wèn)題,那就是需要我們手動(dòng)的全局注冊(cè)。

/* main.js */
import Component1 from '@/component/compenent1'
import Component2 from '@/component/compenent2'
import Component3 from '@/component/compenent3'

Vue.component('component1', Component1)
Vue.component('component2', Component2)
Vue.component('component3', Component3)

當(dāng)組件多了以后,手動(dòng)注冊(cè)也變得繁瑣起來(lái),可以通過(guò)require.context()實(shí)現(xiàn)自動(dòng)注冊(cè)組件。

/**
 * main.js
 * 讀取componetns下的vue文件并自動(dòng)注冊(cè)全局組件
 */
const requireComponent = require.context('./components', false, /\.vue$/)

requireComponent.keys().forEach(fileName => {

 const componentConfig = requireComponent(fileName)
 const componentName = fileName.replace(/^\.\//, '').replace(/\.vue/, '')

 Vue.component(componentName, componentConfig.default || componentConfig)
})

4. 自動(dòng)注冊(cè)vuex模塊

之前我們是這么注冊(cè)vuex模塊的

/* module.js */

import alarm from './modules/alarm'
import history from './modules/history'
import factory from './modules/factory'
import contact from './modules/contact'
import company from './modules/company';
import deviceManage from './modules/device-manage'
import deviceModel from './modules/device-model'
import deviceActivation from './modules/device-activation'
import user from './modules/user'
import role from './modules/role'
import setAlarm from './modules/setAlarm'
import factoryMode from "./modules/factoryMode";
import ScreenDeviceWatch from './modules/screen-device-watch'
import ScreenDeviceForecast from './modules/screen-device-forecast'

export default {
 alarm,
 company,
 deviceManage,
 deviceModel,
 user,
 factory,
 contact,
 deviceActivation,
 history,
 role,
 setAlarm,
 factoryMode,
 ScreenDeviceWatch,
 ScreenDeviceForecast,
}

/* index.js */
import Vue from 'vue'
import Vuex from 'vuex'

import state from './state'
import getters from './getters'
import modules from './modules'
import actions from './actions'
import mutations from './mutations'

Vue.use(Vuex)
export default new Vuex.Store({
 state,
 getters,
 mutations,
 actions,
 modules
})

可以發(fā)現(xiàn)每個(gè)模塊都要我們手動(dòng)導(dǎo)入,然后加入到module里面,如此重復(fù)。當(dāng)模塊不多還好,假如項(xiàng)目大了,有50個(gè)模塊,那就得要做很多重復(fù)的工作。

跟注冊(cè)組件一樣,我們還是利用require.context來(lái)實(shí)現(xiàn)。

/**
 * 讀取./modules下的所有js文件并注冊(cè)模塊
 */
const requireModule = require.context('./modules', false, /\.js$/)
const modules = {}

requireModule.keys().forEach(fileName => {
 const moduleName = fileName.replace(/(\.\/|\.js)/g, '') 
 modules[moduleName] = {
  namespaced: true,
  ...requireModule(fileName).default
 }
})

export default modules

/* index.js */
import Vue from 'vue'
import Vuex from 'vuex'
import modules from './modules'

Vue.use(Vuex)
export default new Vuex.Store({
 state,
 getters,
 mutations,
 actions,
 modules
})

這篇關(guān)于如何寫(xiě)出更優(yōu)雅的vue.js代碼的文章就介紹到這了,希望大家以后多多支持腳本之家。

相關(guān)文章

  • Vue-axios-post數(shù)據(jù)后端接不到問(wèn)題解決

    Vue-axios-post數(shù)據(jù)后端接不到問(wèn)題解決

    這篇文章主要介紹了Vue-axios-post數(shù)據(jù)后端接不到問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • 完美解決vue中報(bào)錯(cuò)?“TypeError:?Cannot?read?properties?of?null?(reading'forEach')“

    完美解決vue中報(bào)錯(cuò)?“TypeError:?Cannot?read?properties?of?null?

    這篇文章主要介紹了完美解決vue中報(bào)錯(cuò)?“TypeError:?Cannot?read?properties?of?null?(reading?‘forEach‘)“,本文給大家分享詳細(xì)解決方案,需要的朋友可以參考下
    2023-02-02
  • 一文詳解Vue中可重用組件的3個(gè)主要問(wèn)題

    一文詳解Vue中可重用組件的3個(gè)主要問(wèn)題

    當(dāng)我們談?wù)摶蛴懻撛?Vue?中創(chuàng)建用戶界面組件時(shí),經(jīng)常會(huì)提到可重用性,在?Vue?中創(chuàng)建真正的可重用組件可能很棘手,在本文中,我將探討可重用組件的概念、應(yīng)用這些組件時(shí)面臨的問(wèn)題,以及為什么必須盡可能克服這些問(wèn)題,需要的朋友可以參考下
    2023-10-10
  • 詳解Vue自定義過(guò)濾器的實(shí)現(xiàn)

    詳解Vue自定義過(guò)濾器的實(shí)現(xiàn)

    這篇文章主要介紹了詳解Vue自定義過(guò)濾器的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
    2017-01-01
  • vue.js層疊輪播效果的實(shí)例代碼

    vue.js層疊輪播效果的實(shí)例代碼

    這篇文章主要介紹了vue.js層疊輪播效果,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2018-11-11
  • vue實(shí)現(xiàn)抽屜彈窗效果

    vue實(shí)現(xiàn)抽屜彈窗效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)抽屜彈窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Vue傳參一籮筐(頁(yè)面、組件)

    Vue傳參一籮筐(頁(yè)面、組件)

    這篇文章主要介紹了Vue傳參一籮筐(頁(yè)面、組件),Vue頁(yè)面、組件之間傳參方式繁多,此處羅列出常用的幾種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-04-04
  • 基于Vue3與免費(fèi)滿血版DeepSeek實(shí)現(xiàn)無(wú)限滾動(dòng)+懶加載+瀑布流模塊及優(yōu)化過(guò)程

    基于Vue3與免費(fèi)滿血版DeepSeek實(shí)現(xiàn)無(wú)限滾動(dòng)+懶加載+瀑布流模塊及優(yōu)化過(guò)程

    在進(jìn)行非完全標(biāo)準(zhǔn)化數(shù)據(jù)的可視化展示時(shí),瀑布流是一種經(jīng)常被采用的展示方法,瀑布流能夠有效地將不同大小規(guī)格的內(nèi)容以一種相對(duì)規(guī)整的方式呈現(xiàn)出來(lái),本文給大家介紹了基于Vue3與免費(fèi)滿血版DeepSeek實(shí)現(xiàn)無(wú)限滾動(dòng)+懶加載+瀑布流模塊,需要的朋友可以參考下
    2025-03-03
  • Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究

    Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究

    Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 詳解vue-cli3使用

    詳解vue-cli3使用

    這篇文章主要介紹了詳解vue/cli 3使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08

最新評(píng)論

上思县| 怀集县| 卢龙县| 博湖县| 安阳市| 甘肃省| 宿松县| 驻马店市| 兴安盟| 长沙市| 正安县| 卓尼县| 平潭县| 浠水县| 卫辉市| 阳江市| 大厂| 无为县| 淄博市| 上思县| 博爱县| 漳平市| 罗山县| 于都县| 利川市| 湛江市| 绩溪县| 应城市| 新蔡县| 洞头县| 二连浩特市| 肥东县| 淮南市| 沽源县| 内江市| 天等县| 龙江县| 嵩明县| 鹰潭市| 专栏| 会理县|