我是 Vue Js 初学者,刚刚使用 Symfony 4 开始使用 Vue Js,但我在 Vue.js 中的表单渲染方面遇到了麻烦。
应用程序.js
import Vue from 'vue'
import App from './Components/App.vue'
import PostForm from './Components/PostForm.vue'
new Vue({
el: '#app',
components: { App },
PostForm
})
PostForm.vue
<template>
<div>
<h2 class="text-center">Post</h2>
<div id="post-form-holder" ref="form">
</div>
</div>
</template>
<script>
import axios from 'axoios'
export default {
async mounted(){
let {data} = await axios.get('/SymVue/post')
this.refs.form.innerHtml = data.form
}
}
</script>
Controller
/**
*
* @Route("/post", name="post.form")
*/
public function formAction(){
$form = $this->createForm(PostType::class);
$view = $this->renderView('vue/form.html.twig',[
'form' => $form->createView()
]);
return new Response('This is response');
}
运行时http://localhost:8000/SymVue/post ,浏览器显示响应消息“这是响应”
最佳答案
如果你运行http://localhost:8000/SymVue/post那么显然你会在浏览器中看到响应。据我所知应该有两种方法。就你而言。第一个方法将在浏览器中呈现表单,第二个方法将创建表单...
像这样......
/**
* @Route("/", name="vue", methods={"GET", "POST"})
*/
public function index()
{
return $this->render('vue/index.html.twig');
}
/**
*
* @Route("/post", name="post.form")
*/
public function formAction(){
$form = $this->createForm(PostType::class);
$view = $this->renderView('vue/form.html.twig',[
'form' => $form->createView()
]);
return new Response('This is response');
}
谈论你的 app.js 我猜你已经搞砸了,但无论如何,如果你想在 PostForm.vue 中渲染,你会做这样的事情。在js文件夹中创建post.js并编写以下代码..
import Vue from 'vue'
import PostForm from './Components/PostForm.vue'
new Vue({
template: '<PostForm />',
components: { PostForm }
}).$mount('#postForm');
在你的PostForm.vue中(在你的代码中出现了一些拼写错误)
import axios from 'axios'
import 'babel-polyfill'
export default {
name: 'PostForm',
async mounted(){
let {data} = await axios.get('/SymVue/post')
this.$refs.form.innerHTML = data.form
}
}
现在回到 vue/index.html.twig 中的 Twig 模板
{% block body %}
<div id="postForm">
<PostForm />
</div>
{% endblock %}
{% block javascripts %}
<script src="{{ asset('build/postForm.js') }}"></script>
{% endblock %}
不要忘记在 webpack.config.js 中添加条目
.addEntry('postForm', './assets/js/post.js')
关于vue.js - 如何在 Vue.js 中渲染 Symfony 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513870/