本文參考自油管上某個國外大神的公開演講視頻,學習了一下以爲很不錯,因此在項目中也使用了這些不錯的技巧。 前端
1. watch 與 computed 的巧妙結合vue
如上圖,一個簡單的列表頁面。vuex
你可能會這麼作:bash
created(){
this.fetchData()
},
watch: {
keyword(){
this.fetchData()
}
}
複製代碼
若是參數比較多,好比上圖函數
可能會是這樣:性能
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)
}
}
複製代碼
前端全棧學習交流圈:866109386,面向1-3經驗年前端開發人員,幫助突破技術瓶頸,提高思惟能力 羣內有大量PDF可供自取,更有乾貨實戰項目視頻進羣免費領取。學習
不過這麼寫,明顯有問題,主要是watch
了不少參數,並且函數的處理都差很少,能夠修改一下,經過methods
處理fetch
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()
}
//前端全棧學習交流圈:866109386
//面向1-3經驗年前端開發人員
//幫助突破技術瓶頸,提高思惟能力
複製代碼
固然這麼寫,須要在模板裏面每一個參數change的地方綁定事件,並傳遞參數值,好比分頁change時:ui
<el-pagination
layout="total, prev, pager, next, jumper"
:total="total"
prev-text="上一頁"
next-text="下一頁"
@current-change="paramsChange('page',$event)"
>
</el-pagination>
複製代碼
相比上面的各類watch,代碼明顯少了不少,可是還有一個問題,那就是要在template的不少地方綁定change
事件。this
最後,固然是使用咱們重點推薦的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
}
},
//前端全棧學習交流圈:866109386
//面向1-3經驗年前端開發人員
//幫助突破技術瓶頸,提高思惟能力
複製代碼
經過增長一個computed屬性,watch這個屬性並設置immediate爲true,無需再手動綁定事件,相比之上的方法都要簡潔。固然,缺點就是對性能稍微有些影響,不過問題不大。
2. 使用mixin提取公共部分
不少列表頁其實使用的不少屬性都是同樣的,好比
這些公共的部分其實能夠經過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>
複製代碼
當有多個頁面須要用到這些組件時,那麼就須要在每一個須要的頁面重複這些步驟。
爲了簡化這些步驟,能夠考慮把這些組件做爲全局組件來使用,這樣每一個頁面須要時,就能夠直接使用了。
不過還有一個問題,那就是須要咱們手動的全局註冊。
/* 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)
複製代碼
當組件多了之後,手動註冊也變得繁瑣起來,能夠經過require.context()
實現自動註冊組件。
/**
* 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
})
複製代碼
能夠發現每一個模塊都要咱們手動導入,而後加入到module裏面,如此重複。當模塊很少還好,假如項目大了,有50個模塊,那就得要作不少重複的工做。
跟註冊組件同樣,咱們仍是利用require.context
來實現。
/**
* 讀取./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
})
複製代碼