0đź‘Ť
VueJS is a client-side-only library, while PHP is server-side. You cannot mix things directly, but since you’re using wordpress you can “localize” your javascript.
If you’re using tools like webpack or browserify, then you should localize your bundled app.js and/or chunk-vendor.js, otherwise you can localize vue.js itself directly, like this (assuming you’re working with a theme)
In your functions.php, write
function my_enqueue_scripts() {
wp_enqueue_script(
'my_js_script_handle',
get_template_directory_uri() . 'js/app.js', // <<---- your Vue bundled code
array(),
false,
true // <<--- Enqueue in footer
);
/**
* Add initial data to your Vue JS object.
*
* @var array
*/
$data = array(
'currentWpUser' => wp_get_current_user()->user_login,
);
wp_localize_script( 'my_js_script_handle', 'PhpDataObject', $data ); // <<--- localize your script
}
add_action ('wp_enqueue_scripts', 'my_enqueue_scripts');
and in your client-side javascript consume your PHP generated data like this (assuming you’re using tools like vue-cli or vueify)
<div id="app">
<p>hey there {{ currentWpUser }} welcome back!</p>
</div>
new Vue({
el: '#app',
data: {
currentWpUser: PhpDataObject.currentWpUser
}
});
-1đź‘Ť
Try this:
<div v-if="ent.Account === '<?php $current_user->user_login ?>' " v-for="ent in leaders">
hey there {{ ent.Name }} welcome back!
</div>
- [Vuejs]-Adding rows in html vue js based on the select option?
- [Vuejs]-V-html does not work with text from database
Source:stackexchange.com