简体   繁体   English

如何使用来自 Axios GET 的 Vue 语法渲染 HTML 字符串?

[英]How to render HTML string with Vue syntax coming from Axios GET?

I have a Symfony 5 and Vue 3 app, a Symfony controller is creating a form and providing its HTML through a JSON response, which is gibing me the form HTML as a string like so我有一个Symfony 5Vue 3应用程序,一个 Symfony 控制器正在创建一个表单并通过 JSON 响应提供它的 HTML,这让我将表单 HTML 作为一个字符串像这样

if ($request->isXmlHttpRequest()) {
    $view = $this->renderView('vue/regForm.html.twig',[
        'form' => $form->createView()
    ]);

    return new JsonResponse([
        'form' => $view,
    ]);
}

In my Register.vue file, I have在我的 Register.vue 文件中,我有

<template>
    <h1>Selected Plan</h1>
    <div id="post-form-holder" ref="form" v-on:submit.prevent="onSubmit"></div>
</template>

<script>
    export default {
        async mounted() {
            let { data } = await this.$http.get("/register/get/" + this.$route.params.plan);
            this.$refs.form.innerHTML = data.form
        },
    };
</script>

This is all working and the form is output onto the page successfully etc. My problem comes when I add some Vue syntax to the form in regForm.html.twig file, (eg. v-bind:class="{ 'has-error': hasError }" ) the Vue syntax obviously does get rendered when using innderHTML.这一切正常,表单成功输出到页面等。当我在 regForm.html.twig 文件中的表单中添加一些 Vue 语法时,我的问题出现了(例如v-bind:class="{ 'has-error': hasError }" ) Vue 语法显然在使用 inderHTML 时会被渲染。

Basically, how can I get the Vue syntax within regform.html.twig to render when the form is placed on the page in Vue 3.基本上,当表单放置在 Vue 3 的页面上时,如何在regform.html.twig中获取 Vue 语法来呈现。

There is the v-html directive.v-html指令。 Check this https://v2.vuejs.org/v2/guide/syntax.html#Raw-HTML检查这个https://v2.vuejs.org/v2/guide/syntax.html#Raw-HTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM