我在 Vue 路由器中构建了以下代码,它在 Vue-CLI 中完美运行。
import store from "./../../store/index.js";
function getView(view) {
return () => import(`@/views/settings/${view}.vue`);
}
const routes = [
{
path: "/myProfile",
name: "MyProfile",
component: getView("MyProfile"),
beforeEnter: (to, from, next) => {
document.title = "Profile - " + store.getters.getAppName;
if (store.getters["userStore/authenticated"]) {
next();
} else {
next({ name: "Home" });
}
},
}
]
export default routes;
现在我用 Vite 替换 Vue-CLI,它给出了以下错误。
TypeError: Failed to resolve module specifier '@/views/settings/MyProfile.vue'
当我删除 getView("MyProfile") 函数并直接使用 import 时,它可以工作。
const routes = [
{
path: "/myProfile",
name: "MyProfile",
component: () => import('@/views/settings/MyProfile.vue'),
beforeEnter: (to, from, next) => {
document.title = "Profile - " + store.getters.getAppName;
if (store.getters["userStore/authenticated"]) {
next();
} else {
next({ name: "Home" });
}
},
}
]
有人可以解释一下为什么吗?