1

我创建了一个包含Encore Webpack的Symfony 4项目。我的项目中的微博页面模板如下所示。如您所见,我使用holder.js来显示图像占位符,但它们根本不显示。

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

{% block body %}
    {% for post in posts %}
        <div class="media text-muted pt-3">
            <img data-src="holder.js/32x32?text=MJ&bg=e83e8c&fg=fff&size=8" alt="" class="mr-2 rounded">
            <p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
                <span class="d-block"><strong class="text-gray-dark">@username</strong> <small>{{ post.time|date("d/m/y") }}</small></span>
                {{ post.text }}
            </p>
        </div>
    {% endfor %}
{% endblock %}

页面呈现没有图像 页示例

webpack.config.js

    .addEntry('app', [
        './node_modules/jquery/dist/jquery.slim.js',
        './node_modules/popper.js/dist/popper.min.js',
        './node_modules/bootstrap/dist/js/bootstrap.min.js',
        './node_modules/holderjs/holder.min.js'
        ])
    .addStyleEntry('css/app', [
        './node_modules/bootstrap/dist/css/bootstrap.min.css',
        './assets/css/app.css'
    ])
4

2 回答 2

1

给出了错误的app.js路径- build\js\app.js。通过向基本模板添加有效的app.js路径来解决:

base.html.twig

...

{% block javascripts %}
    <script src="{{ asset('build/app.js') }}"></script>
{% endblock %}

</body>

于 2019-02-01T10:39:33.937 回答
0

一切看起来都很好,但是您没有包含整个 webpack 配置。只需确保在 webpack.config.js 文件中注释掉以下行:

//.enableSingleRuntimeChunk()

完成后,再次运行 encore,然后持有人应该正确渲染图像。

vagrant@homestead:~/badland-retail$ ./node_modules/.bin/encore dev

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

Encore
// directory where compiled assets will be stored
.setOutputPath('public/build/')
// public path used by the web server to access the output path
.setPublicPath('/build')
// only needed for CDN's or sub-directory deploy
//.setManifestKeyPrefix('build/')

/*
 * ENTRY CONFIG
 *
 * Add 1 entry for each "page" of your app
 * (including one that's included on every page - e.g. "app")
 *
 * Each entry will result in one JavaScript file (e.g. app.js)
 * and one CSS file (e.g. app.css) if you JavaScript imports CSS.
 */
.addEntry('js/app',
    [
        './node_modules/jquery/dist/jquery.slim.js',
        './node_modules/popper.js/dist/popper.min.js',
        './node_modules/bootstrap/dist/js/bootstrap.min.js',
        './node_modules/holderjs/holder.min.js'
    ])

.addStyleEntry('css/app', [
    './node_modules/bootstrap/dist/css/bootstrap.min.css',
    './assets/css/app.css'
    ])

// will require an extra script tag for runtime.js
// but, you probably want this, unless you're building a single-page app
//.enableSingleRuntimeChunk()

/*
 * FEATURE CONFIG
 *
 * Enable & configure other features below. For a full
 * list of features, see:
 * https://symfony.com/doc/current/frontend.html#adding-more-features
 */
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
// enables hashed filenames (e.g. app.abc123.css)
.enableVersioning(Encore.isProduction())

// enables Sass/SCSS support
//.enableSassLoader()

// uncomment if you use TypeScript
//.enableTypeScriptLoader()

// uncomment if you're having problems with a jQuery plugin
//.autoProvidejQuery()

// uncomment if you use API Platform Admin (composer req api-admin)
//.enableReactPreset()
//.addEntry('admin', './assets/js/admin.js')
;

module.exports = Encore.getWebpackConfig();
于 2019-01-30T10:03:24.443 回答