【问题标题】:Symfony Vue.js Encore Failed to mount component: template or render function not definedSymfony Vue.js Encore 无法挂载组件:未定义模板或渲染函数
【发布时间】:2023-03-24 14:32:02
【问题描述】:

我正在尝试使用 symfony 设置 vue.js。 (这个 Vue.js 配置在 laravel 中正常工作,但 symfony 没有,我必须为这个项目使用 symfony :()

从 symfony 文档配置的 webpack.config.js

var Encore = require('@symfony/webpack-encore');

Encore
    // the project directory where all compiled assets will be stored
    .setOutputPath('web/build/')

    // the public path used by the web server to access the previous directory
    .setPublicPath('/build')

    // will create web/build/app.js and web/build/app.css
    .addEntry('vue', './web/assets/js/vue.js')

    .enableVueLoader()
    ...
;

// export the final configuration
module.exports = Encore.getWebpackConfig();

web/assets/js/vue.js 中的 vue.js

import Vue from 'vue'

Vue.component('example-component', require('./components/Example.vue'));

const app = new Vue({
  el: '#app'
})

示例组件:

<template>
    <div>
        Hello!
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

Twig 视图:(id 为 app 的 div 在 base.html.twig 中)

{% extends 'base.html.twig' %}
{% block content %}

    <example-component></example-component>

    ...
{% endblock %}
{% block jsvendors %}
{% endblock %}

构建编译成功,但在控制台中仍然出现此错误:

[Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <ExampleComponent>
       <Root>

我做错了什么? 我不想像在 vue-loader 示例中那样将 vue 用作简单的页面应用程序,我只需要在不同的页面部分添加几个组件,就像在 laravel 中一样。

谢谢大家。

【问题讨论】:

  • 你安装vue-template-compiler了吗?
  • 当然 :) Vue、vue-loader vue-template-compiler 和 webpack-encore 都安装好了。

标签: symfony npm vue.js


【解决方案1】:

尝试将默认值添加到要求:

Vue.component('example-component', require('./components/Example.vue').default);

【讨论】:

    猜你喜欢
    • 2019-07-02
    • 1970-01-01
    • 2020-06-19
    • 2020-05-12
    • 2017-02-13
    • 2017-07-26
    • 2018-07-16
    • 2020-02-02
    • 2017-02-25
    相关资源
    最近更新 更多