在挂载的 div 中加载 Vue 3 组件并传递 PHP 数据 [英] Load Vue 3 Component inside mounted div and pass PHP data

查看:26
本文介绍了在挂载的 div 中加载 Vue 3 组件并传递 PHP 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事一个新项目,我必须将 Vue 3 集成到一个大型 Symfony/Drupal 项目中.该项目已经包含了很多 PHP 代码,实际上我不想重构太多.

好吧,我尝试设置一小段 Vue 代码,看看如何开始处理其余代码.实际上我只是想将一些 PHP 代码从 index.html.twig 传输到 sidebar.vue 文件.我也顺便从 Symfony 使用 Webpack Encore.我读到我可以使用 Vue 组件来实现这一点,但我的组件没有加载到我的 <div id="app"></div> 中.或者至少不是我希望它们加载的方式.

webpack.config.js (Webpack Encore)

var Encore = require('@symfony/webpack-encore');如果 (!Encore.isRuntimeEnvironmentConfigured()) {Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');}再来一次.setOutputPath('webroot/public/build/').setPublicPath('/public/build').addEntry('main', './vue/src/main.js').splitEntryChunks().enableSingleRuntimeChunk().cleanupOutputBeforeBuild().enableBuildNotifications().enableSourceMaps(!Encore.isProduction()).enableVersioning(Encore.isProduction()).configureBabelPresetEnv((config) => {config.useBuiltIns = '用法';config.corejs = 3;}).enableSassLoader().enablePostCssLoader()//启用 Vue.enableVueLoader(() => {}, {版本:3,运行时编译器构建:假,});;module.exports = Encore.getWebpackConfig();

ma​​in.js

import { createApp } from 'vue';从'./components/sidebar'导入侧边栏;const app = createApp({})app.component('sidebar', Sidebar);app.mount("#app");

sidebar.vue