报错提示
Failed to resolve component: router-view
If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement
1. 项目源代码
1.1配置路由:src / router / index.js
//引入路由
import { createRouter, createWebHashHistory } from "vue-router";
//定义路由
const routes = [
{
path: '/',
component:()=>import('../view/Main'),
children: [
{
path:'/',
name: 'home',
component: () => import('../view/home/Home')
}
]
}
]
const router = createRouter({
history: createWebHashHistory(),
routes
})
//导出路由
export default router
1.2入口文件引入路由
import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index'
const app = createApp(App)
app.use(router)
// createApp(App).mount('#app') 原错误
//上方已经定义 createApp,下面直接用即可
app.mount('#app')
1.3路由占位即跳转
App.vue
<template>
<router-view></router-view>
</template>
<script setup>
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
src / view / Main.vue
<template>
<div>
左侧菜单
</div>
<div>
头部
</div>
<div>
随时发生变化
<router-view></router-view>
</div>
</template>
src / home / Home.vue
<template>
<div>
我是首页组件
</div>
</template>