①form装饰器
当默认的表单验证不能满足需求时,就可以在rules属性里添加一个validator,然后在methods中写自定义规则的方法。
// 自定义校验函数,要求输入的是一个正整数
handlePasswordLevel(rule, value, callback) {
const number = Number(value);
if (!Number.isInteger(number) || number < 0) {
// 如果需要返回 error msg,就把它传给 `callback()`
callback('请输入大于等于0的整数');
} else {
// 如果通过校验,调用无参数的 `callback()` 即可
callback();
}
}
②http请求
this.$http.get('url')
.then((res)=>{ })//接口调用成功返回的数据
.catch((err)=>{ })//接口调用失败返回的数据
this.$http.get('url').then(res=>{ }).catch(err=>{ })
this.$http.get('url').then(r=>{ }).catch(err=>{ })
③mutation-type.js
vuex中建立的mutation-type.js专门来放mutation 里用到的方法常量值
即在mutation-type.js中定义值在mutations中设置值
④actions中的{ commit }
用于提交mutations的方法
普通写法是context.commit('方法名','参数')
传入参数context
简写可以写成commit('方法名','参数')
传入参数{ commit }
Login({ commit }, userInfo) {
return new Promise((resolve, reject) => {
login(userInfo).then(response => {
if (response.code == '200') {
const result = response.result
const userInfo = result.userInfo
Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
commit('SET_TOKEN', result.token)
commit('SET_INFO', userInfo)
commit('SET_NAME', { username: userInfo.username, realname: userInfo.realname, welcome: welcome() })
commit('SET_AVATAR', userInfo.avatar)
resolve(response)
} else {
reject(response)
}
}).catch(error => {
reject(error)
})
})
},
vuex中的mutations是存放处理数据方法的集合。在使用时需要commit,mutations中不能存在异步操作。actions存在的意义就是进行异步操作,异步操作出结果后再去commit mutation。action提交的是mutation,而不是直接变更状态。action可以包含任何异步操作。调用action即在组件里dispatch actions中的方法。
⑤vue3.0以下版本安装npm install -g vue-cli@2.9.6
vue3.0以上版本安装cnpm install -g @vue/cli@3.11.0
⑥路由守卫router.beaforeEach
router.beforeEach(({meta, path}, from, next) => {}
或router.beforeEach((to, from, next) => {