VueCli 使用 render + jsx 报错:You may need an additional loader to handle the result of these loaders

在Vue2.x环境下,使用VueCli时遇到jsx报错:Module parse failed: Unexpected token。错误源于缺少处理jsx的额外loader。解决办法是安装babel-plugin-transform-vue-jsx相关插件,并在babel.config.js中配置。安装命令包括babel-plugin-syntax-jsx等,然后确保项目已安装@vue/cli-plugin-babel。配置完成后,问题得以解决。

原文链接
Vue Render HTML - You may need an additional loader to handle the result of these loaders

环境:Vue 2.x

在 VueCli 中使用 render + jsx 报错:

Module parse failed: Unexpected token (224:36)
File was processed with these loaders:
 * ./node_modules/cache-loader/dist/cjs.js
 * ./node_modules/vue-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.

报错代码:

headings: {
  id: 'ID',
  selected: function (h) {
    return <b-form-checkbox v-model={this.checkedAll} onChange={this.selectAll}>
          </b-form-checkbox>
  }
}

解决方法:
安装 babel 插件:https://github.com/vuejs/babel-plugin-transform-vue-jsx
在这里插入图片描述
安装命令:

npm install babel-plugin-syntax-jsx babel-plugin-transform-vue-jsx babel-helper-vue-jsx-merge-props babel-preset-env --save-dev

在 babel.config.js 配置:

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ]
}

每天都要被百度的辣鸡搜索结果气死,最后还是通过不可访问的搜索引擎,几秒搜到解决方案。stackoverflow YYDS
注:配置babel.config.js的前提是,项目安装了@vue/cli-plugin-babel,我使用的是@3.7.0版本。

PS D:\java\web\study\vue3-project> npm run serve > vue3-project@0.1.0 serve > vue-cli-service serve INFO Starting development server... ERROR Failed to compile with 3 errors 11:05:47 [eslint] D:\java\web\study\vue3-project\src\views\HomeView.vue 79:0 error Parsing error: Unexpected reserved word 'interface'. (8:0) ✖ 1 problem (1 error, 0 warnings) You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file. error in ./src/views/HomeView.vue?vue&type=script&lang=ts&setup=true Module parse failed: The keyword 'interface' is reserved (7:0) File was processed with these loaders: * ./node_modules/vue-loader/dist/index.js You may need an additional loader to handle the result of these loaders. | | > interface Tree { | [key: string]: any | } error in ./src/views/HomeView.vue?vue&type=template&id=9ea40744&scoped=true&ts=true Module parse failed: Unexpected token (5:27) File was processed with these loaders: * ./node_modules/vue-loader/dist/templateLoader.js * ./node_modules/vue-loader/dist/index.js You may need an additional loader to handle the result of these loaders. | const _hoisted_1 = { class: "layout-container-demo" } | > export function render(_ctx: any,_cache: any,$props: any,$setup: any,$data: any,$options: any) { | const _component_el_input = _resolveComponent("el-input")! | const _component_el_tree = _resolveComponent("el-tree")! ERROR in ./src/views/HomeView.vue?vue&type=script&lang=ts&setup=true (./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/HomeView.vue?vue&type=script&lang=ts&setup=true) 7:0 Module parse failed: The keyword 'interface' is reserved (7:0) File was processed with these loaders: * ./node_modules/vue-loader/dist/index.js You may need an additional loader to handle the result of these loaders. | | > interface Tree { | [key: string]: any | } @ ./src/views/HomeView.vue?vue&type=script&lang=ts&setup=true 1:0-141 1:0-141 1:142-272 1:142-272 @ ./src/views/HomeView.vue 2:0-70 8:49-55 3:0-65 3:0-65 @ ./src/main.js 2:0-39 6:22-25 ERROR in ./src/views/HomeView.vue?vue&type=template&id=9ea40744&scoped=true&ts=true (./node_modules/vue-loader/dist/templateLoader.js??ruleSet[1].rules[3]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/HomeView.vue?vue&type=template&id=9ea40744&scoped=true&ts=true) 5:27 Module parse failed: Unexpected token (5:27) File was processed with these loaders: * ./node_modules/vue-loader/dist/templateLoader.js * ./node_modules/vue-loader/dist/index.js You may need an additional loader to handle the result of these loaders. | const _hoisted_1 = { class: "layout-container-demo" } | > export function render(_ctx: any,_cache: any,$props: any,$setup: any,$data: any,$options: any) { | const _component_el_input = _resolveComponent("el-input")! | const _component_el_tree = _resolveComponent("el-tree")! @ ./src/views/HomeView.vue?vue&type=template&id=9ea40744&scoped=true&ts=true 1:0-219 1:0-219 @ ./src/views/HomeView.vue 1:0-89 8:68-74 19:29-35 18:88-20:3 18:2-20:4 @ ./src/main.js 2:0-39 6:22-25 ERROR in [eslint] D:\java\web\study\vue3-project\src\views\HomeView.vue 79:0 error Parsing error: Unexpected reserved word 'interface'. (8:0) ✖ 1 problem (1 error, 0 warnings) webpack compiled with 3 errors什么问题
11-15
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值