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 }

猜你喜欢

转载自blog.csdn.net/weixin_50587417/article/details/128040584
今日推荐