javascript - Is it possible to render a PHP frontend into a Vue node?

Solution:

Maybe you need like to : a module php or component as template.php(php server)

    export const templateOfAdvanceTemplatePage = `
  <div class="content edit-page management">
    <md-card class="page-card">
     <?php echo "My Component" ?>
    </md-card>
  </div>

And from node server

     import * as url from "url";

var templateOfAdvanceTemplatePage = url.parse("http://www.website.com/template.php");
    
    export default {
      template: templateOfAdvanceTemplatePage,
      ...
    }

for more information import vue here, and php as javascript file here

Answer

Solution:

Vue.js can be used in two separate ways: For more complex applications you would use a build process and pre-compile the templates from the source, which are usually Single File Components SFC; *.vue files. The templates would then become render functions and no HTML is ending up in the output assets. There is, however, another way of defining Vue components. You can define them inline with the runtime-only bundle of Vue. For migrations and smaller applications this approach would be advised. You would need to include the compiler. See also the Vue documentation about that topic Vue v2 and Vue v3). If you are importing Vue as a module and are missing the compiler, see here.

If you want to render dynamically generated HTML from PHP as a Vue template, you would need the second approach. Keep in mind that, with this approach, you would always need to have the generated PHP output to be in sync with the Vue components. And you would need to fully trust the HTML, you are generating with PHP, otherwise you will risk injections.

There is, however, still another problem: You need the generated PHP output HTML as a string within JavaScript and it should not be interpreted by the browser (ideally) or removed again from the DOM. So, you need to decide (based on your project) how you want to generate the HTML so that it can be read in as a JavaScript string. Here are some approaches:

  1. Generate the HTML directly into the page. Then, define which element you want to target, get the HTML with .innerHTML and delete the node from HTML (drawback: you will render the HTML twice, might produce short visual glitches).
  2. Fetch the HTML via XHR from a separate page. You will directly have the HTML as a string in the response (see e.g. ).
  3. Render <script type="text/x-template" id="static-html-content"></script> around the generated HTML content. Then, you do not need the HTML as string and you can directly use the id as reference (use template: '#static-html-content'). See the documentation of X-Templates in Vue.

Then, you can use the runtime-only version of Vue and define your components. Here is a live example:

const Counter = {
  // retrieve and add your template string here
  template: `
  <div class="counter">
    This is a counter: {{ counter }}
    <button @click="counter++">Increase Counter</button>
  </div>
  `,
  data: function() {
    return {
      counter: 0
    }
  }
};

const App = {
  components: { Counter },
  template: `
    <div class="app">
      This is the app component.
      <hr />
      <counter />
    </div>
  `
};

new Vue({
  el: '#element',
  template: '<App />',
  components: { App }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="element"></div>

Source