Ask Your Question
3

How can I make Vue components appear in a blade.php file in Laravel?

asked 2023-05-17 23:07:21 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-17 23:20:02 +0000

devzero gravatar image

To make Vue components appear in a blade.php file in Laravel, follow these steps:

  1. Install Vue.js and Vuex using npm.

  2. Create a Vue component in resources/js/components directory. For example, create the file MyComponent.vue, and add the following code:

<template>
  <div>
    <h1>Hello, {{ name }}!</h1>
  </div>
</template>

<script>
export default {
  props: {
    name: String,
  },
};
</script>
  1. Register the component globally in app.js file. Open the resources/js/app.js file and add the following code:
import Vue from 'vue'
import MyComponent from './components/MyComponent.vue'

Vue.component('my-component', MyComponent)

const app = new Vue({
    el: '#app'
});
  1. In your blade.php file, add a container div and an id to mount the Vue app to:
<body>
  <div id="app">
    <!-- Vue components will be mounted here -->
  </div>
</body>
  1. Render the component in the blade.php file. For example, to render the MyComponent.vue component, add the following code in the blade.php file:
<my-component name="{{ $name }}"></my-component>

Here, $name is a PHP variable that will be passed as a prop to the MyComponent Vue component.

  1. Finally, compile the assets using Laravel Mix by running the following command:
npm run dev

Now, you should see the Vue component rendered in the blade.php file.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-17 23:07:21 +0000

Seen: 11 times

Last updated: May 17 '23