javascript - Nuxt.js动态组件错误 "Either pre-compile the templates into render functions, or use the compiler-included build"

标签 javascript vue.js nuxt.js

我在 Nuxt.js 中收到以下错误:

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

found in

---> <Anonymous>
       <RenderPost> at components/RenderPost.vue
         <Pages/post/Id.vue> at pages/post/_id.vue
           <Nuxt>
             <Layouts/default.vue> at layouts/default.vue
               <Root>

我正在遵循此处的示例:https://stackoverflow.com/a/39519105和我的RenderPost.vue大致看起来像这样:

<template>
    <client-only>
        <component :is="dynamicComponent" />
    </client-only>
</template>

<script>
export default {
    methods:
    {
        linkedView()
        {
            return `<a href="#" @click.prevent="runSomething">Click me</a>`;
        },

    },
    computed :
    {
        dynamicComponent() {
            return {
                data() { return { foo : null }},
                template : `<div>${this.linkedView()}<br>{{ foo }}</div>`,
                methods :
                {
                    runSomething()
                    {
                        this.foo = 'ran something!'
                    }
                }
            }
        }
    }
}
</script>

我添加了<client-only>因为我还收到有关服务器和客户端不匹配的错误。如果没有它,我会收到一个额外的错误:

[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside <p>, or missing <tbody>. Bailing hydration and performing full client-side render.

最佳答案

Nuxt 通常仅包含 Vue 运行时(不包括编译器)作为优化,可将构建大小减少约 10KB,因为大多数用户使用预编译模板(例如,通过单个文件组件)。 Vue 仅运行时构建会在运行时使用 in-DOM 或字符串模板时发出您观察到的警告。

由于您的应用在运行时需要字符串模板,因此您需要配置 Nuxt 以使用 Vue build that includes the compiler :

// nuxt.config.js
export default {
  build: {
    extend(config) {
      config.resolve.alias.vue = 'vue/dist/vue.common'
    }
  },
}

关于javascript - Nuxt.js动态组件错误 "Either pre-compile the templates into render functions, or use the compiler-included build",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65253323/

相关文章:

javascript - Firefox 忽略表单提交的 JavaScript 覆盖

typescript - VueJS字符串插值可以访问TS对象的私有(private)属性

javascript - 将事件绑定(bind)到多个对象

javascript - Vue.js 2.5.16 中数字/字符串的 Prop 行为不一致

javascript - 如何在servicenow中从javascript访问果冻变量

javascript - TinyMCE 弹出窗口在 Chrome 中太小

javascript - 如何使用 Axios 向 Stripe API 发出 https 请求?

vue.js - 使用 Vuejs 事件中心发出时传递参数

vue.js - 如何更新 vuetify mdi 图标? (Nuxt.js)

vue.js - 在 nuxt.js 中登录后重定向到以前的 url