1.vue移動(dòng)端項(xiàng)目中遇到的問(wèn)題
window.scrollTo(0, 0) //頁(yè)面切換時(shí)強(qiáng)制滾動(dòng)到頂端
//多個(gè)接口報(bào)錯(cuò)時(shí),避免報(bào)錯(cuò)信息重疊
async fetchData() {
this.isAllFetchError = false
await Promise.all([
this.fetchPassenger(),//在這三個(gè)接口返回的對(duì)應(yīng)的錯(cuò)誤code碼中設(shè)置isAllFetchError==true
this.fetchTrend(),
this.fetchWdPlazasInfo()
])
if (this.isAllFetchError) {
alert('運(yùn)行錯(cuò)誤')//錯(cuò)誤信息
}
this.isAllFetchError = false
},//多個(gè)接口數(shù)據(jù)為空時(shí),錯(cuò)誤信息只提示一次
vue移動(dòng)端rem設(shè)置
data() {
return {
screenWidth: document.body.clientWidth,
timer: false
}
},
created() {
this.calRem()
window.onresize = () => {
return (() => {
window.screenWidth = document.body.clientWidth
this.screenWidth = window.screenWidth
this.calRem()
})()
}
},
watch: {
screenWidth(val) {
if (!this.timer) {
this.screenWidth = val
this.timer = true
setTimeout(() => {
this.calRem()
this.timer = false
}, 400)
}
}
},
methods: {
calRem() {
const docEl = document.documentElement
const rem = this.screenWidth / 3.75
docEl.style.fontSize = rem + 'px'
},
}
2.axios的設(shè)置
axios的作用是什么呢:axios主要是用于向后臺(tái)發(fā)起請(qǐng)求的,還有在請(qǐng)求中做更多可控功能。
特點(diǎn):支持瀏覽器和node.js
- 支持promise
- 能攔截請(qǐng)求和響應(yīng)
- 能轉(zhuǎn)換請(qǐng)求和響應(yīng)數(shù)據(jù)
- 能取消請(qǐng)求
- 自動(dòng)轉(zhuǎn)換JSON數(shù)據(jù)
- 瀏覽器支持防止CSRF(跨站請(qǐng)求偽造)
3.export和export default以及export as
1、export與export default均可用于導(dǎo)出常量、函數(shù)、文件、模塊等
2、你可以在其它文件或模塊中通過(guò)import+(常量 | 函數(shù) | 文件 | 模塊)名的方式,將其導(dǎo)入,以便能夠?qū)ζ溥M(jìn)行使用
3、在一個(gè)文件或模塊中,export、import可以有多個(gè),export default僅有一個(gè)
4、通過(guò)export方式導(dǎo)出,在導(dǎo)入時(shí)要加{ },export default則不需要
4.根據(jù)登錄時(shí)的用戶角色,顯示相應(yīng)的頁(yè)面
main.js
const getRouterName = function(allArray, obj) {
if (obj.children && obj.children.length > 0) {
for (let i = 0; i < obj.children.length; i++) {
allArray.push(obj.children[i].name)
getRouterName(allArray, obj.children[i])
}
} else {
return
}
}
//定義一個(gè)全局方法,返回true則表示能進(jìn)入該頁(yè)面,false則把跳轉(zhuǎn)路由的按鈕置灰
Vue.prototype.$targetPathAuth = function(targetPath) {
let userRouter = []
getRouterName(userRouter, store.getters.addRouters[0])
return userRouter.indexOf(targetPath) === -1
}
使用:
<a :disabled="edit" @click="editAlgorithm(record.id)">編輯</a>
data(){
return {
edit: false,
}
},
created() {
this.edit = this.$targetPathAuth('EditAlgorithm')
},
5.在頁(yè)面下鉆及上鉆時(shí)可能會(huì)改變多個(gè)vuex變量,可把多個(gè)commit動(dòng)作合并成一個(gè)dispatch
/** @format */
const actions = {
drillUp({ commit }, payload) {
commit('updateDefaultOrgName', payload.name)
commit('upDrillPath')
commit('updateLevelCode', payload.orgLevelCode)
commit('updateTabShowStatus', true || payload.tabShowStatus)
},
drillDown({ commit }, payload) {
commit('downDrillPath', {
name: payload.name,
id: payload.id
})
commit('updateDefaultOrgName', payload.defaultOrgName)
commit('updateLevelCode', 'plaza')
commit('updateTabShowStatus', payload.tabShowStatus)
}
}
export default actions
//用法
this.$store.dispatch('drillDown', {
name: this.$store.state.defaultOrgName,
id: this.$store.state.workingOrgCode,
defaultOrgName: item.plazaName,
singlePlazaSignal: true,
tabShowStatus: false
})
6.vue項(xiàng)目git提交代碼自動(dòng)格式化
package.json中
"scripts": {
"lint": "vue-cli-service lint",
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended",
"@vue/prettier"
],
"parserOptions": {
"ecmaVersion": 2020
},
"rules": {
"no-console": 0,
"no-useless-escape": 0,
"no-multiple-empty-lines": [
2,
{
"max": 3
}
],
"prettier/prettier": [
"error",
{
"singleQuote": true,
"semi": false,
"trailingComma": "none",
"bracketSpacing": true,
"jsxBracketSameLine": true,
"insertPragma": true,
"requirePragma": false
}
]
}
},
"gitHooks": {
"pre-commit": "lint-staged"
},
"lint-staged": {
"*.{js,vue}": [
"prettier --write",
"vue-cli-service lint --fix",
"git add"
]
}
7.mixin和provider/inject
8.異步神器async-await介紹
9.vue-router 中的導(dǎo)航鉤子
10.keep-alive組件的使用及其實(shí)現(xiàn)原理
11.store的數(shù)據(jù)流流程
12.數(shù)據(jù)更新時(shí)怎么使用store
13.實(shí)現(xiàn)只想 router-view 里面某個(gè)組件被緩存?
參考:http://www.itdecent.cn/p/0b0222954483
關(guān)鍵點(diǎn):使用 router.meta 擴(kuò)展
14.watch用法
watch用來(lái)監(jiān)聽(tīng)在頁(yè)面中捕捉不到的變化,如果能在頁(yè)面方法及計(jì)算屬性中拿到值,就不需要使用watch方法