I
I
Ilya Bobkov2019-06-13 14:18:46
Vue.js
Ilya Bobkov, 2019-06-13 14:18:46

How to render a vue component with props on the backend?

Good day!
There was a question about rendering a vue component on the backend.
There is a vue component with props that should display a list of incoming data from the backend:

<template>
<div v-for="(item,index) in items :key=index">
 {{ item }}
</div>
</template>
<script>
export default {
  props: ["items"] // входящие данные с бэкенда
}
</script>

There is a server part in PHP (Laravel):
private function render() {
  $renderer_source = File::get(base_path('node_modules/vue-server-renderer/basic.js'));
  $app_source = File::get(public_path('js/mycomponent.js'));
  $v8 = new \V8Js();
  ob_start();

  $js= 'var process = { env: { VUE_ENV: "server", NODE_ENV: "production" } };  this.global = { 
 process: process };';
 
$v8->executeString($js);
 $v8->executeString($renderer_source);

$code="

var propsData=["1", "2", "3"];

// Не понятно как передать массив propsData в компонент 
renderVueComponentToString(??? propsData ???, (err, html) => {
    	print(html)
 })";"

$v8->executeString($code);

return view('index', ['ssr' => ob_get_clean()]); // передаю данные на фронт
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex Wells, 2019-06-13
@Alex_Wells

Why render in php? Raise the node, write an SSR server there and let your backend pull as much as you like. v8 in php is some kind of game.

A
Alexander Aksentiev, 2019-06-13
@Sanasol

You first create what you will render, otherwise some kind of mess from copy-paste of different lines.
https://ssr.vuejs.org/guide/non-node.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question