简体   繁体   English

Vue.js中的备用绑定语法

[英]Alternative binding syntax in Vue.js

Question

I want to know if there is an alternative syntax to output data in Vue.js , instead of the curly braces, like the ng-bind Angular directive . 我想知道是否有一种替代语法来输出Vue.js中的数据 ,而不是花括号, 比如ng-bind Angular指令

Reading the docs, it seems that Vue.js accepts only tag properties with the v-bind directive , but I want it to work with the inner html too. 阅读文档,似乎Vue.js只接受带有v-bind指令的 标签属性 ,但我希望它也能用于内部html。

Context 上下文

I want to output data using PHP and, once the page is loaded, manage it with Vue. 我想使用PHP输出数据,一旦加载页面,就用Vue管理它。 Imagine the next situation: 想象一下下一个情况:

We want this output: 我们想要这个输出:

<div>Hello</div>

First, we output the data with php 首先,我们用php输出数据

<div><?php echo $hello_string ?></div>

After that, we want to be able to change the content with Vue. 之后,我们希望能够使用Vue更改内容。 The current syntax is; 目前的语法是;

<div>{{ hello_string }}</div>

We can't mix the two syntaxes, so I need something like this: 我们不能混合使用这两种语法,所以我需要这样的东西:

<!--Ideal syntax for mixing vue and php-->
<div v-bind:innerhtml="hello_string"><?php echo $hello_string ?></div>

Thank you for your help. 谢谢您的帮助。

You could use the v-text directive: 您可以使用v-text指令:

<div v-text="hello_string"></div>
<!-- same as -->
<div>{{ hello_string }}</div>

or the v-html : 或者v-html

<div v-html="html"></div>
<!-- same as -->
<div>{{{ html }}}</div>
Vue.component({
    el:'#app',
    data:function(){
        return {
            hello_string:"<?php echo json_encode($hello_string) ?>"
        };
    }
});

Then in HTML: 然后在HTML中:

<div id="app><div>{{ hello_string }}</div></div>

Basically you need to use PHP to fill your javascript variables, whether you do it through AJAX or just printing out the variables like above is up to you. 基本上你需要使用PHP来填充你的javascript变量,无论你是通过AJAX还是只打印出上面的变量都取决于你。 You probably need to encode it to JSON or at least make sure quotes are escaped. 您可能需要将其编码为JSON或至少确保引用被转义。 On the front end, let Vuejs manage the view rather than printing directly into it with PHP. 在前端,让Vuejs管理视图而不是使用PHP直接打印到视图中。

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

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