Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

vue3 new joiner, why @click not work on a button in follow vue3 codes

Following codes, I am confused why add1 function not work on button click? please note codes are saved as html file and run in chrome.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello Vue</title>
    <script src="https://unpkg.com/vue@3"></script>
</head>

<body>

<div id="app">
    <!-- TODO: this is the place for my quesiton, why add1 doesn't work -->
    <button @click="add1">
        Hello, count is {{count}}
    </button>
</div>

</body>
</html>

<script>

    const app = Vue.createApp({
            data() {
                return {
                    count: 0
                }
            }
        },

        methods = {
            add1() {
                alert("Click!")
                count++
            }
        }
    )
    app.mount('#app')

</script>

The warn/error msg i see in google developer console is: Property "add1" was accessed during render but is not defined on instance. and, Extraneous non-props attributes (add1) were passed to component but could not be automatically inherited because component renders fragment or text root nodes.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your app declaration has syntax errors and does not include methods. Try:

const app = Vue.createApp({
  data () {
    return {
      count: 0
    }
  },
  methods: {
    add1 () {
      alert("Click!")
      count++
    }
  }
})
app.mount('#app')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading