2

i want render Vue.js Code but NOT WORKING IN LARAVEL 5.7

My app.js Code :

require('./bootstrap');

window.Vue = require('vue');

Vue.component('hello-world-component', require('./components/HelloWorld.vue'));

const app = new Vue({
    el: '#app'
});

And New File HelloWorld.vue :

<template>
  <div class="container">
    Hello Laravel + Vue!
  </div>
</template>

<script>
  export default {
    mounted() {
      console.log('Component mounted.')
    }
  }
</script>

And welcome.blade.php :

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <title>Laravel</title>
    </head>
    <body>
        <div class="content">
            <div class="title m-b-md">
                <hello-world-component></hello-world-component>
            </div>
        </div>
    </body>
</html>

when i run this command,

sudo npm run dev

result :

DONE  Compiled successfully in 6224ms                                                                   7:16:51 AM
       Asset     Size  Chunks                    Chunk Names
  /js/app.js  1.38 MB       0  [emitted]  [big]  /js/app
/css/app.css   198 kB       0  [emitted]         /js/app

but when i connect my index page, not showing my HelloWorld.Vue code.

i have no idea what is problem?

4 Answers 4

3

You need an element with id app to mount to. For example:

html

<body>
    <div id="app"></div>
</body>
<script src="/js/app.js"></script>

js

const app = new Vue({
  el: '#app',
  template: '<hello-world-component></hello-world-component>'
});
Sign up to request clarification or add additional context in comments.

Comments

1

put an id="app" to your div

    <body>
        <div id="app" class="content">
            <div class="title m-b-md">
                <hello-world-component></hello-world-component>
            </div>
        </div>
    </body>

Comments

1

As DigitalDrifter wrote add id="app" and also add path to your compiled js assets.

Something like this (better before </body> tag).

<script src="{{asset('/js/app.js')}}" crossorigin="anonymous"></script>

Messages in your console can also provide you with some clues for further debugging.

Comments

0

You did not added id app in your blade

also you did not included script file which is compiled

Update your blade

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <title>Laravel</title>
    </head>
    <body>
        <div class="content" id="app">
            <div class="title m-b-md">
                <hello-world-component></hello-world-component>
            </div>
        </div>
    </body>
<script src="{{ asset('js/app.js') }}"></script>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.