A
A
ArtJH2019-07-11 16:55:53
Vue.js
ArtJH, 2019-07-11 16:55:53

How to assign vue variable value to php variable?

var app = new Vue({
el: '.blog',
data: {
mes : 'there should be a value from php'
}
});

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
kodwi, 2019-07-12
@ArtJH

Extremely wrong approach to development! Do not mix warm with soft. Vue is purely front-end, it should only communicate with the php back-end through API requests. The fact that you generate the front code on the back is a very outdated approach, you don’t need to write like this now from the word at all. So frontend frameworks do not use!

R
Roman Chuvilev, 2019-07-12
@FrontEndTarot

PHP => HTML data attribute => Vue.

N
Nikita Dergachov, 2019-07-11
@vanillathunder

It's better not to do this, but it's possible

var app = new Vue({
el: '.blog',
data: {
mes : '<?= $var ?>'
}
});

E
Eugene, 2019-07-11
@redmo

Recently there was the same task, on WordPress I screwed Vue. To pass a variable from php, I used the wp_localize_script function, in short, it will output something similar

<script type='text/javascript'>
/* <![CDATA[ */
var object_name = {"some_string":"Some string to translate","a_value":"10"};
/* ]]> *//code>

В соседней ветке рекомендуют так выводить на странице
<code lang="php">
echo sprintf("<script>var json_data = '%s';</script>", json_encode($arr));
</code>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question