vitepress/vite vue3 怎么实现vue模版字符串实时编译

如果是vue模版字符串的话,先解析成模版对象

另一篇文章里有vue模版字符串解析成vue模版对象-CSDN博客

            //vue3写法(vue2可以用new Vue.extend(vue模版对象)来实现)

            import { createApp, defineComponent } from 'vue';

           // 定义一个简单的Vue组件
            const MyComponent = defineComponent(
                 //vue 模版对象
                {
                template: `<div>这是动态创建的Vue实例</div>`
                }
            );
            
            const app = createApp(MyComponent);;
            // 挂载组件到 #display这个dom节点下
            app.mount('#display');
出现报错

[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 <App>

通常是因为 VitePress 的默认配置不支持在运行时编译 Vue 模板。VitePress 使用的是 Vite 作为其构建工具,而 Vite 默认配置是为了生产环境优化,不包含完整的 Vue 编译器。

要解决这个问题,需要确保 Vite 能够处理 .vue 文件中的模板,并且正确地导入 Vue。在 VitePress 项目中,可以通过修改 Vite 的配置来做到这一点。

vitepress项目配置

// .vitepress/config.js   
export default defineConfig({
  // 其他 VitePress 配置...
  vite: {
    // Vite 配置对象
    optimizeDeps: {
      include: ['vue'], // 确保 Vue 被包括在预构建依赖中
    },
    resolve: {
      alias: {
        // 配置 Vue 的别名,指向包含编译器的版本
        vue: 'vue/dist/vue.esm-bundler.js',
      },
    },
    // 如果你需要其他 Vite 配置,也可以在这里添加
  },
});

vite项目配置

// vite.config.js
export default {
  resolve: {
    alias: {
      vue: 'vue/dist/vue.esm-bundler.js'
    }
  }
}

效果

相关推荐

  1. Vue3实现响应式编程

    2024-04-11 14:38:06       32 阅读
  2. VueVuex模块编码(非常实用

    2024-04-11 14:38:06       29 阅读
  3. vue3 模板编译过程

    2024-04-11 14:38:06       19 阅读
  4. vue3+ts实现表格单元格编辑功能

    2024-04-11 14:38:06       9 阅读
  5. 深入C语言库:字符字符串函数模拟实现

    2024-04-11 14:38:06       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-11 14:38:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-11 14:38:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-11 14:38:06       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-11 14:38:06       20 阅读

热门阅读

  1. 使用Python快速模拟前端常用页面数据格式

    2024-04-11 14:38:06       16 阅读
  2. 单例模式基本介绍及两种实现方式详解

    2024-04-11 14:38:06       14 阅读
  3. 鸿蒙开发 一 (二)、熟悉鸿蒙之剑 ArkTS

    2024-04-11 14:38:06       18 阅读
  4. 腾讯云短暂崩溃2小时

    2024-04-11 14:38:06       16 阅读
  5. MySQL | 加索引报错

    2024-04-11 14:38:06       15 阅读
  6. 算法刷题day44

    2024-04-11 14:38:06       14 阅读
  7. 如何使用pandoc转word和正确的参考文献格式

    2024-04-11 14:38:06       13 阅读
  8. AcWing 793. 高精度乘法——算法基础课题解

    2024-04-11 14:38:06       14 阅读
  9. node 的路径分析和文件查找策略

    2024-04-11 14:38:06       13 阅读