英文:
Why the error runs - Uncaught SyntaxError: The requested module '/src/router/index.js' does not provide an export named 'default' at main.js vue?
问题
At vite vue runs the error - Why the error runs - Uncaught SyntaxError: The requested module '/src/router/index.js' does not provide an export named 'default' at main.js vue ? What is wrong ?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// main.js
import { createApp } from 'vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap/dist/js/bootstrap.js'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')
// router index.js
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';
import Shops from '../views/Shops.vue';
const router = VueRouter.createRouter({
history: VueRouter.createWebHashHistory(),
routes: [
{
path: '/',
name: 'Home',
component: Home,
},
{
path: '/shops',
name: 'Shops',
component: Shops,
},
],
});
export default router;
<!-- end snippet -->
英文:
At vite vue runs the error - Why the error runs - Uncaught SyntaxError: The requested module '/src/router/index.js' does not provide an export named 'default' at main.js vue ? What is wrong ?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// main.js
import { createApp } from 'vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap/dist/js/bootstrap.js'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')
// router index.js
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';
import Shops from '../views/Shops.vue';
const router = VueRouter.createRouter({
history: VueRouter.createWebHashHistory(),
routes: [
{
path: '/',
name: 'Home',
component: Home,
},
{
path: '/shops',
name: 'Shops',
component: Shops,
},
],
});
export default router;
<!-- end snippet -->
答案1
得分: 0
请分享 /src/router/index.js
的代码。从您通过 stackblitz 分享的代码中,我看到 router 文件夹内的 index.js 是空的。
请在 router/index.js 内添加一个导出,例如参考以下内容:
export default expression;
export default function functionName() { /* … */ }
export default class ClassName { /* … */ }
export default function* generatorFunctionName() { /* … */ }
export default function () { /* … */ }
export default class { /* … */ }
export default function* () { /* … */ }
这些都是您可以使用的导出默认的方式。请参考 MDN 文档以了解更多关于如何使用导出的信息:
https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export
编辑解决方案:
在 router/index.js 中更新为 import * as VueRouter from 'vue-router';
,应该可以解决问题。这个错误是因为 vue-router 使用了命名导出而不是默认导出所引起的。
英文:
Can you please share /src/router/index.js
code? What I can see in the code you have shared via stackblitz is index.js inside router folder is empty.
Please add an export inside router/index.js, for example refer below
export default expression;
export default function functionName() { /* … */ }
export default class ClassName { /* … */ }
export default function* generatorFunctionName() { /* … */ }
export default function () { /* … */ }
export default class { /* … */ }
export default function* () { /* … */ }
These are all types of export default you can use..
Please refer MDN Docs to know more about how to use export
https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export
Edit Solution :
import * as VueRouter from 'vue-router';
Update this in router/index.js and it should work. This error occurs because vue-router has a named export instead of a default export
答案2
得分: 0
你必须以以下方式声明路由并从路由文件(src/router/index.js)中导出。
import { createRouter, createWebHistory } from "vue-router";
const routers = {
{ path: '/', name: 'NameOfTheComponent', component: () => import('组件路径') }
}
const router = createRouter({ history: createWebHistory(), routes: routers });
export default router;
正如你所看到的,最后一行将定义的路由文件导出到你想要导入并用作路由的组件中。
英文:
you must declare the router in the following manner and export from the router file (src/router/index.js).
import { createRouter, createWebHistory } from "vue-router";
const routers={
{path:'/',name:'NameOfTheComponent',component:()=>import('pathof the component')
}
const router=createRouter({history:createWebHistory(),routers});
export default router;
as you see the last line is exporting the defined router file to the the component that you want to import and use it as a router.
答案3
得分: 0
我认为你忘记在路由器代码的末尾添加
export default router;
英文:
I think you forget to put
export default router;
At the end of the router code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论