记一次 Injection elForm not found错误原因分析

本文解决Vue项目中使用自定义表单设计工具form-generator遇到的问题,包括Runtime与Compiler模式的区别,如何正确引入ElementUI,以及配置vue.config.js以支持自定义组件的template配置。

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

   需求:

最近在入坑vue前后端分离方案,要实现在后端实现自定义表单设计(用的form-generator),然后提交自定义表单模板到数据库(以json和vue两种格式保存)。前端再给用户提供自定义表单(模板来自数据库)并提交数据。

 实现:

demo模仿前端渲染自定义表单

关键点在于import Vue from 'vue/dist/vue.js' 

一开始用的是import Vue from 'vue', 还是报错[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

这里涉及Runtime 模式和Compiler模式的问题,去度娘找了半天参考了https://blog.youkuaiyun.com/sleepwalker_1992/article/details/82082400,然后改成了import Vue from 'vue/dist/vue.js' ,改成这个之后再把Custom.vue的

import Element from 'element-ui';
Vue.use(Element) 

抽到全局main.js,果然还是报错

我以为是Custom.vue无法引用到elementui,再把 

import Element from 'element-ui';
Vue.use(Element) 拷回Custom.vue,

找了几天百度,问了各种论坛,终于发现问题还是出现在运行模式,只要注册自定义组件里面出现template配置,就一定用的是编译Compiler模式。

 

 Compiler模式附资料http://nferzhuang.com/vue%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90%E5%87%86%E5%A4%87-runtime-only%E5%92%8Cruntime-compiler/

最终修改vue.config.js(如果不存在则手动创建),修改为

runtimeCompiler:true,问题成功解决
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值