-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
关于获取用户信息的字段能重写吗,,我总是报错 #2831
Comments
这是info接口
|
请问问题解决了吗,我也报同样的错 |
作为初学Vue时曾经被这个问题所困扰过的一员,今天无意中发现还是有很多人仍然被这个问题所困扰着。所以决定对这个问题进行下详细的分析拆解,以便让后续遇到这个问题的人能够直接定位并修改问题。具体见下。 问题现象登录时报错,见上面的截图。主要是:
这里摘录成文字,主要是为了方面后续查找这个问题时能够搜索到。 问题分析这个问题主要还是报错内容和实际原因关联性不强,所以才难以定位的。 // src\permission.js
// 从第37行起
const { roles } = await store.dispatch('user/getInfo') // 自己写的user/getInfo的返回结果中不包含roles,导致roles的值是undefined。
// generate accessible routes map based on roles
const accessRoutes = await store.dispatch('permission/generateRoutes', roles) // 进入 src\store\modules\permission.js 第50行。 // src\store\modules\permission.js
// 从第49行起
const actions = {
generateRoutes({ commit }, roles) {
return new Promise(resolve => {
let accessedRoutes
if (roles.includes('admin')) { // 由于roles的值是undefined,这里会报错。进入 src\permission.js 第48行的异常处理分支。
accessedRoutes = asyncRoutes || []
} else {
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
}
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
})
}
} // src\permission.js
// 从第48行起
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error') // 这里的error是一个Error对象,不是一个字符串。Message.error会将其当做Vue组件的data,导致出现问题现象中的报错。
next(`/login?redirect=${to.path}`)
NProgress.done()
} 解决方案
Message.error(error || 'Has Error') 修改为 Message.error(error.message || 'Has Error') 或者 Message.error({ message: error } || 'Has Error') 也就是说,Message.error的第一个参数要么是一个字符串,要么是一个设置Message属性的Object。不能是其他Object。 相关问题扩展阅读:为什么Message.error的第一个参数是Error对象会报错参见element-ui的相关源码,仅摘录关键部分 \\ packages\message\src\main.js
import Main from './main.vue';
let MessageConstructor = Vue.extend(Main);
const Message = function(options) {
if (Vue.prototype.$isServer) return;
options = options || {};
if (typeof options === 'string') {
options = {
message: options
};
}
instance = new MessageConstructor({
data: options // 将options作为了MessageConstructor的data。
});
instance.id = id;
if (isVNode(instance.message)) {
instance.$slots.default = [instance.message];
instance.message = null;
}
instance.$mount();
document.body.appendChild(instance.$el);
let verticalOffset = options.offset || 20;
instances.forEach(item => {
verticalOffset += item.$el.offsetHeight + 16;
});
instance.verticalOffset = verticalOffset;
instance.visible = true;
instance.$el.style.zIndex = PopupManager.nextZIndex();
instances.push(instance);
return instance;
};
['success', 'warning', 'info', 'error'].forEach(type => {
Message[type] = options => {
if (typeof options === 'string') {
options = {
message: options
};
}
options.type = type;
return Message(options); // 如果Message.error的参数options不是字符串类型,就直接将其传给Message了。
};
}); |
good! |
good |
good! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我自己研究了两天都没弄明白,就是关于获取用户信息接口那里,我可以用自己写的字段么。
这是花裤衩大大写的关于用户信息的每个字段
我想改成自己的,那需要修改哪几个地方。。。
The text was updated successfully, but these errors were encountered: