【无标题】Error:Failed to resolve component: router-view

在Vue项目中遇到一个错误提示,表示无法解析组件router-view。问题可能出在路由配置或导入上。在src/router/index.js中正确配置了路由,包括Main和Home组件。同时,在App.vue中使用<router-view/>作为占位以实现页面跳转。在入口文件中引入并应用了路由。Main.vue和Home.vue分别定义了组件内容。解决方法可能涉及检查编译选项,确保对自定义元素的处理设置正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错提示
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>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值