英文:
TypeError: a.then is not a function while compiling in production
问题
我正在尝试使用npm run build
(即vite build)在生产环境中编译我的Vue应用程序。
之后,我尝试使用/dist文件夹在服务器上提供我的应用程序,一切似乎都运行得很完美,我能够发送fetch请求,单击各种链接等等。
不幸的是,在登录后,当应该重定向时,我收到以下错误:
TypeError:在生产编译时a.then不是一个函数
以及
未捕获的(在承诺中)TypeError:a.then不是一个函数
在开发模式下,一切都运行得非常正常,但在生产环境中却不起作用。
似乎与路由有关
这是我的路由代码:
const state = reactive({
token: localStorage.getItem("token"),
userAdmin: false,
userRestaurateur: false,
userDeliverer: false
});
if (state.token) {
const user = JSON.parse(atob(state.token.split('.'[1]))
state.userAdmin = Object.values(user.roles).includes('ROLE_ADMIN');
state.userRestaurateur = Object.values(user.roles).includes('ROLE_RESTAURANT');
state.userDeliverer = Object.values(user.roles).includes('ROLE_DELIVERER');
}
const router = createRouter({
history: createWebHistory('/'),
routes: [
// ...(你的其他路由配置)
],
});
我尝试检查其他方法是否正确运行,尝试更改服务器,但似乎都不起作用。
英文:
I'm trying to compile my vue app in production with npm run build
(which is vite build).
After that I try to serve my app on my server with the /dist folder and everything seems to be working perfectly, I'm able to send fetch request, click on various links etc.
Unfortunately, after logging in and when I should be redirected, I'm getting the error
> TypeError: a.then is not a function while compiling in production
and
> Uncaught (in promise) TypeError: a.then is not a function"
Everything just works perfectly fine while I'm in dev mode, it's just not working in production.
It seems to be linked to the router
This is the code for my Router :
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
const state = reactive({
token: localStorage.getItem("token"),
userAdmin: false,
userRestaurateur: false,
userDeliverer: false
});
if (state.token) {
const user = JSON.parse(atob(state.token.split('.')[1]))
state.userAdmin = Object.values(user.roles).includes('ROLE_ADMIN');
state.userRestaurateur = Object.values(user.roles).includes('ROLE_RESTAURANT');
state.userDeliverer = Object.values(user.roles).includes('ROLE_DELIVERER');
}
const router = createRouter({
history: createWebHistory('/'),
routes: [
{
path: '/',
name: 'home',
component: function () {
if (state.token && state.userAdmin) {
return Users
} else if (state.token && state.userRestaurateur) {
return HomeRestaurateur
}else if (state.token && state.userDeliverer) {
return Commands
}else {
return Home
}
}
},
{
path: "/login",
name: "login",
component: Login,
},
{
path: "/forgot-password",
name: "forgot_password",
component: ForgotPassword,
},
{
path: "/reset-password/:token",
name: "reset_password_token",
component: ResetPassword,
},
{
path: "/register",
name: "register",
component: Register,
},
{
path: "/profile",
name: "editProfile",
component: editProfile,
},
{
path: "/Restaurant/:id/Menu",
name: "Meals",
component: Meals,
},
{
path: "/admin/users",
name: "admin_users",
component: function () {
if (state.userAdmin) {
return Users
} else {
return Error403
}
}
},
{
path: "/admin/restaurants",
name: "admin_restaurants",
component: function () {
if (state.userAdmin) {
return Restaurants
} else {
return Error403
}
}
},
{
path: "/admin/restaurants_request",
name: "admin_restaurants_request",
component: function () {
if (state.userAdmin) {
return RestaurantsRequest
} else {
return Error403
}
}
},
{
path: "/restaurants/new",
name: "create_restaurants",
component: CreateRestaurant,
},
{
path: "/admin/reports",
name: "admin_reports",
component: function () {
if (state.userAdmin) {
return Reports
} else {
return Error403
}
}
},
{
path: "/orders",
name: "orders",
component: Commands,
},
{
path: "/:pathMatch(.*)*",
name: "not_found",
component: Error404,
}
],
});
<!-- end snippet -->
I tried checking if other methods were working correctly, tried to change server, nothing just seems to work.
答案1
得分: 1
以下是您要翻译的内容:
我遇到了同样的问题。我的本地构建运行正常,但在构建生产版本并部署到服务器后,出现了类似的错误。
我的控制台错误信息如下:
Uncaught (in promise) TypeError: i.then is not a function
堆栈跟踪指向了vue-router。我尝试了很多方法,直到看到了您的评论。在我的路由中,我为组件使用了函数,像这样:
{
path: '/profile',
name: 'Profile',
component: () => Profile, // 这是问题所在
},
我将其更改为:
{
path: '/profile',
name: 'Profile',
component: Profile, // 这是修复方法
},
现在它可以正常工作了!
英文:
I was having the same problem. My local build was working fine, but after building for production and deploying to my server, I was getting a similar error.
My console error was
Uncaught (in promise) TypeError: i.then is not a function"
The stacktrace pointed to vue-router. I tried many things until I saw your comment. In my routes, I had functions for the components like:
{
path: '/profile',
name: 'Profile',
component: () => Profile, //this was the problem
},
I changed it to:
{
path: '/profile',
name: 'Profile',
component: Profile, //this was the fix
},
and now it is working!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论