• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

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

    來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:13:23
    文檔

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

    幾個你不知道的技巧助你寫出更優(yōu)雅的vue.js代碼: 1. watch 與 computed 的巧妙結(jié)合 如上圖,一個簡單的列表頁面。 你可能會這么做: created(){ this.fetchData() }, watch: { keyword(){ this.fetchData() } } 如果參數(shù)比較多,比如上圖 關(guān)鍵字篩選, 區(qū)域篩選, 設(shè)備ID篩選, 分頁數(shù), 每頁
    推薦度:
    導(dǎo)讀幾個你不知道的技巧助你寫出更優(yōu)雅的vue.js代碼: 1. watch 與 computed 的巧妙結(jié)合 如上圖,一個簡單的列表頁面。 你可能會這么做: created(){ this.fetchData() }, watch: { keyword(){ this.fetchData() } } 如果參數(shù)比較多,比如上圖 關(guān)鍵字篩選, 區(qū)域篩選, 設(shè)備ID篩選, 分頁數(shù), 每頁

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

    如上圖,一個簡單的列表頁面。

    你可能會這么做:

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

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

  • 關(guān)鍵字篩選,
  • 區(qū)域篩選,
  • 設(shè)備ID篩選,
  • 分頁數(shù),
  • 每頁幾條數(shù)據(jù),
  • 可能會是這樣:

    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)
     }
    }

    不過這么寫,明顯有問題,主要是watch了很多參數(shù),而且函數(shù)的處理都差不多,可以修改一下,通過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)然這么寫,需要在模板里面每個參數(shù)change的地方綁定事件,并傳遞參數(shù)值,比如分頁change時:

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

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

    最后,當(dāng)然是使用我們重點推薦的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
     }
    },

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

    2. 使用mixin提取公共部分

    很多列表頁其實使用的很多屬性都是一樣的,比如

  • 分頁 page
  • 數(shù)量 size
  • 搜索關(guān)鍵 字keyword
  • 表格數(shù)據(jù) tableData
  • 這些公共的部分其實可以通過mixin來提取出來

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

    在要用到的頁面

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

    3. 自動注冊全局組件

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

    <template>
     <all-region :selectRegion="selectRegion" @region-change="selectRegion=$event"/>
    </template>
    
    <script>
    import AllRegion from './baseButton'
    
    export default {
     components: {
     AllRegion,
     }
    }
    </script> 

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

    為了簡化這些步驟,可以考慮把這些組件作為全局組件來使用,這樣每個頁面需要時,就可以直接使用了。

    不過還有一個問題,那就是需要我們手動的全局注冊。

    /* 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)組件多了以后,手動注冊也變得繁瑣起來,可以通過require.context()實現(xiàn)自動注冊組件。

    /**
     * main.js
     * 讀取componetns下的vue文件并自動注冊全局組件
     */
    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. 自動注冊vuex模塊

    之前我們是這么注冊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)每個模塊都要我們手動導(dǎo)入,然后加入到module里面,如此重復(fù)。當(dāng)模塊不多還好,假如項目大了,有50個模塊,那就得要做很多重復(fù)的工作。

    跟注冊組件一樣,我們還是利用require.context來實現(xiàn)。

    /**
     * 讀取./modules下的所有js文件并注冊模塊
     */
    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)于如何寫出更優(yōu)雅的vue.js代碼的文章就介紹到這了,希望大家以后多多支持腳本之家。

    聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

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

    幾個你不知道的技巧助你寫出更優(yōu)雅的vue.js代碼: 1. watch 與 computed 的巧妙結(jié)合 如上圖,一個簡單的列表頁面。 你可能會這么做: created(){ this.fetchData() }, watch: { keyword(){ this.fetchData() } } 如果參數(shù)比較多,比如上圖 關(guān)鍵字篩選, 區(qū)域篩選, 設(shè)備ID篩選, 分頁數(shù), 每頁
    推薦度:
    標簽: 幾個 js 的技巧
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 精品国产一级在线观看| 欧美成人精品网站播放 | 久久www免费人成精品香蕉| CAOPORM国产精品视频免费| 亚洲国产成人乱码精品女人久久久不卡| 久久国产精品一区二区| 久久这里只有精品18| 人妻一区二区三区无码精品一区| 98精品国产自产在线XXXX| 国产精品无圣光一区二区| 亚洲中文字幕久久精品无码APP| 久久精品国产一区二区| 国产福利电影一区二区三区,欧美国产成人精品一 | 成人国产一区二区三区精品 | 久久精品国产欧美日韩| 粉嫩精品美女国产在线观看| 国产精品欧美日韩| 嫩草伊人久久精品少妇AV| 最新精品亚洲成a人在线观看| 日韩福利视频精品专区| 久久91精品综合国产首页| 国产精品亚洲视频| 国产精品电影在线| 国产精品va久久久久久久| 99RE8这里有精品热视频| 亚洲人成亚洲精品| 亚洲国产精品自在线一区二区| 精品久久久久久国产91| 国产成人精品日本亚洲11| 91在线视频精品| 国产精品电影网| 久久这里只有精品久久| 午夜精品美女自拍福到在线| 色综合久久综精品| 国产精品免费视频观看拍拍| 国产精品亚洲欧美大片在线观看| 国产99视频精品免视看7| 99国产欧美精品久久久蜜芽| 热综合一本伊人久久精品| 日韩精品免费一线在线观看| 无码人妻精品一区二区蜜桃百度 |