2

The result is an empty page after {{greeting }} flashes on the screen. I am new to Vue and just wanted to try it in laravel so I made a simple blade:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>VueJS</title>
    <link rel="stylesheet" href="">

</head>
<body>
<div id="app">
    @{{ greeting }}
</div>

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

Then this is js/app.js:

require('./bootstrap');
window.Vue = require('vue');


// const files = require.context('./', true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))

Vue.component('example-component', require('./components/ExampleComponent.vue').default);

const app = new Vue({
    el: '#app',
    data: {
        greeting: 'Hello!'
    }
});

To which I only added the greeting in data. When I load the site I get the above mentioned error or in full:

app.js:38298 [Vue warn]: Property or method "greeting" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties. (found in Root)

I have tried removing the "@" before {{ greeting }}, but that gave me the "Use of undefined constant" error. I have also checked out the link provided in the error and do my best to replicate it in app.js like so:

const app = new Vue({
    el: '#app',
    data: {
        greeting: ''
    },
    template: '<div>{{ greeting }}</div>'
});

app.greeting = 'Hello!';

But got the same error as well. I have also checked out some of the solutions but I wasn't able to understand enough of the solutions to apply it to my problem.

2
  • 1
    just to be sure, after adding the data: property, did you re compile the assets? npm run dev , or npm run watch? Commented Aug 13, 2020 at 0:36
  • Yah, I was wondering this too. I actually did not realize that for non-components, data can actually be defined as above too. Commented Aug 13, 2020 at 0:38

2 Answers 2

3

The issue is that you have not defined the data of your Vue instance correctly. data should be defined as follows:

const app = new Vue({
    el: '#app',
    data() {
        return {
            greeting: 'Hello!';
        };
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

You should define data like this :

const app = new Vue({
el: '#app',
data() {
    return {
        greeting: 'Hello!';
    };
}, 
methods: {
  // methods
}

})

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.