Component provided template option but runtime compilation is not supported in this build of Vue. Co

开发遇到以下警告:

[Vue warn]: Component provided template option but runtime compilation is not supported in this build of Vue. Configure your bundler to alias "vue" to "vue/dist/vue.esm-bundler.js". 
  at <Anonymous onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > > 
  at <RouterView> 
  at <App>

翻译:组件提供的模板选项,但在Vue的这个版本中不支持运行时编译。

 问题:vue3模板渲染模板template不支持编译

问题代码:

import { createRouter, createWebHistory } from 'vue-router'

const Home = { template: '<div>Home</div>' }
const About = { template: '<div>About</div>' }

const router = createRouter({
  history: createWebHistory(),
  // history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/about',
      name: 'about',
      component: About
    }
  ]
})

export default router

解决方式:解决方式有两种

1.第一种解决方式:将路由组件的template修改为render

const Home = { template: '<div>Home</div>' }
const About = { template: '<div>About</div>' }

// 修改为以下方式

const Home = { render(){ return 'Home'} }
const About = { render(){ return 'About'} }

2.第二种解决方式:修改vue项目的webpack.config.js配置文件

vite 构建的项目

alias: {
    'vue': 'vue/dist/vue.esm-bundler.js'
}

vue-cli构建的项目

module.exports = { runtimeCompiler: true }

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值