13

when using the following code: https://github.com/iamshaunjp/vuejs-playlist/blob/lesson-18/src/App.vue

My browser displays function () { [native code] } where it should display "heeey cowboy".

Any idea what's going on? I'm working from the tutorial here, using the CLI, and everything is identical to the provided files.

1
  • Show how you are calling whatever function should be saying "heeey cowboy". You are likely missing either a :, some () or doing something to cause Vue to try to output a literal function as apposed to calling something. Commented Mar 27, 2018 at 16:25

3 Answers 3

31

You forgot the parenthesis mate:

<template>
    <div>
        <h1>{{ title }}</h1>
        <p>{{ greeting() }}</p>
    </div>
</template>

The error is in greeting, you forgot to add these parenthesis after it (), that's how you call a javascript function

Sign up to request clarification or add additional context in comments.

4 Comments

Aha! Funny, in Vanilla I would have caught that. Sometimes, Vue does not require the parenthesis after a method call. Can you explain when/why that is?
Well, it's not about vue then it's about the function being a callback. What happens is you send your function to a parent function which runs it.
I would set greeting to a data or computed prop and call a method whenever you need to change that value -- or better yet, make a reactive computed property (which will update only when it needs to). Prefer to use static variables or data/computed props whenever you can. When you use a method in your template, it will be called repeatedly as the page redraws and I think there are probably very few use cases where that's desirable
5

methods: vs. computed:

In Vue 3, while refactoring parts of main.js into a template, I mistakenly copy/pasted computed: (properties) into methods: in myTemplate.js.

When I re-ran app, I saw:

function () { [native code] }

... instead of return values from functions.

After moving my computed properties from methods: into computed: section of my app.component, the template called the functions properly.

To use Computed Properties in Vue we don't need (), just like get methods.

app.component('your-template', {
    props: {
        someObject: {
            type: Object,
            required: true
        }
    },
    methods: {
        /* METHODS WITH ARGUMENTS - callWith(args) */
    },
    computed: {
        /* PROPERTIES - call without () */

        lastUpdated() {
            let _date = new Date(this.bookshelf.modified)
            return _date.toLocaleString()
        },
    },
    template: 
        /*html*/
        `
        <h3>
            {{ someObject.name }}
        </h3>
        <div>Updated: {{ lastUpdated }}</div>
        `,
})

1 Comment

This should be the accepted answer just because it gives the solution and gives an explanation .
0

In my case it was nothing wrong with my code. I just had to delete node_modules folder, rebuild the app, unregister the service worker, refresh the app and it started to work

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.