0

I try to pass simple php variable to Vuejs component. I found similar problem but solution does't work for me. How to pass a PHP variable to Vue component instance in Laravel blade?

App:

new Vue({
    el: '#app',
    components: { Home },
    template: "<Home />"
})

Home component:

<template>
  <div>
    <Header :userId="$phpUserId"></Header>
  </div>
</template>

<script>
import Header from "./components/Header.vue";
export default {
  name: "Home",
  components: {
    Header
  }
};
</script>

Header component:

<template>
  <header>
    <div>number : {{this.userId}}</div>
  </header>
</template>


<script>
export default {
  name: "Header",
  props: [
    {
      name: "userId",
      default: 0
    }
  ]
};
</script>

PHP simple page:

...
<body>
    <?php $phpUserId = 123; ?>
    <div id="app"></div>
</body>
...

Any solution for this problem? I'm new in vuejs :(

8
  • The other answer should still be valid. Just change {{ $client->id }} (which is the only Blade/Laravel-specific code) with <?= $client->id ?> Commented Dec 13, 2019 at 12:39
  • I don't use Laravel, just simple PHP. @deceze - this is not the same problem. This is vuejs problem - so, please don't close this question and open it. Commented Dec 13, 2019 at 14:02
  • Vue is Javascript. PHP is PHP. There's no fundamental difference. The duplicate still applies. If you think it doesn't, specifically explain why. Commented Dec 13, 2019 at 14:05
  • @deceze - Because Vue is framework and my question is about feature of that framework witch should work but in my case it doesn't. My app is bandle by webpack, so I can't put <?php ?> in my script tag. So, please re-open this thread because someone who uses and knows Vue may have something important to convey that can help me solve my problem. Commented Dec 13, 2019 at 14:27
  • Yeah, so use one of the other solutions, like AJAX. Commented Dec 13, 2019 at 14:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.