Laravel中使用Vue组件的其他方式?无报错Vue组件不加载问题咨询
Laravel集成Vue组件不加载修复方案
- 第一步确认基础资源加载正常
打开浏览器开发者工具的网络面板,确认app.js没有返回404,再看控制台面板有没有隐藏的JS报错。确认你的布局文件layouts/main.blade.php中正确引入了编译后的JS:
修改完代码后必须执行编译命令:<script src="{{ mix('js/app.js') }}"></script>npm run dev,代码变更才会生效。 - 第二步修复App.js中Vue引入冲突
现有代码同时使用require和import两种方式引入Vue,会导致实例异常:
删除window.Vue = require('vue').default;这一行,仅保留import Vue from 'vue';即可(适用于Vue2版本)。 - 第三步确认组件路径和注册规则匹配
核对你的.vue文件路径和引入路径完全一致,注意Linux环境下文件名区分大小写,比如components/audit/index.vue不要写成components/Audit/index.vue。 - 第四步路由兼容配置(可选,后续使用路由跳转时需要)
你使用了history路由模式,需要在routes/web.php中添加兜底路由,避免刷新页面404:Route::get('/{any}', function () { return view('index'); })->where('any', '.*');
如果你的项目使用的是Vue3版本,原有Vue2的挂载和组件注册语法完全不兼容,参考修改App.js如下:
require('./bootstrap'); import { createApp } from 'vue' import { createRouter, createWebHistory } from 'vue-router' import { routes } from './routes'; import ProductInfoIndex from './components/productInfo/index.vue' import AuditIndex from './components/audit/index.vue' const router = createRouter({ history: createWebHistory(), routes: routes }) const app = createApp({}) app.component('productinfo-index', ProductInfoIndex) app.component('audit-index', AuditIndex) app.use(router) app.mount('#app')
内容的提问来源于stack exchange,提问作者Artūras Yapaz
相关产品推荐
相关产品推荐

