我正在尝试将 ReactJS 集成到我的 Symfony 5 项目中,但在导入它之后,它似乎破坏了代码。
我遵循了Symfony 文档和Symfony Casts 系列中提到的步骤:
react
和react-dom
.app_react.js
in的文件assets/js/
,然后将其作为新条目添加到webpack.config.js
with .addEntry('app_react', './assets/js/app_react.js')
。app_react.js
在base.html.twig
与<script src="{{ asset('build/app_react.js') }}"></script>
.enableReactPreset()
templates/user/base.html.twig
:<div id="app-react"></div>
yarn encore dev --watch
(一切都编译)。React
的app_react.js
在此之后,如果我只调试一个简单console.log('Hello world')
的打印,但如果我导入之后React
没有执行代码。我已经尝试过使用 React 打印一些代码的常用方法:
import React from 'react';
import ReactDOM from 'react-dom';
console.log('This is not being printed');
const SomeText = () => (
<h2>Some Text</h2>
);
ReactDOM.render(<SomeText />, document.getElementById('react-app'));
这是浏览器源app_react.js
文件显示的内容:
以防万一,这是我的webpack.config.js
文件:
var Encore = require('@symfony/webpack-encore');
if (!Encore.isRuntimeEnvironmentConfigured()) {
Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}
Encore
.setOutputPath('public/build/')
.setPublicPath('/build')
.copyFiles({
from: './assets/image'
})
.addEntry('app', './assets/js/app.js')
.addEntry('app_react', './assets/js/app_react.js') // Step 3
.autoProvidejQuery()
.splitEntryChunks()
.enableSingleRuntimeChunk()
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.configureBabelPresetEnv((config) => {
config.useBuiltIns = 'usage';
config.corejs = 3;
})
.enableSassLoader()
.enableReactPreset() // Step 5
;
module.exports = Encore.getWebpackConfig();
那么,我在这里做错了什么?
我认为您的问题可能在第 4 步。而不是使用
<script src="{{ asset('build/app_react.js') }}"></script>
应该是:
{{ encore_entry_script_tags('app_react') }}
Webpack encore 将查找manifest.json
以找出要包含的文件。可能不仅仅是app_react.js
文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句