0

Very simple but seems to not be working. Must be because i'm new to VueJS. I downloaded this repo https://github.com/creotip/vue-particles. As I want to create a under construction page using this style.

Problem: I need to create a hamburger menu icon in the top right hand corner which on click calls a method to hide and show a div ( really basic stuff ). I've followed the vue JS tutorials and what people have said on stack overflow but I just cant get my template to speak to the method.

When I click on the hamburger button i keep getting "_vm.hello is not a function". What am i doing wrong? Please see screenshot. There must be something simple to solve this.

Heres what my code looks like:-

app.vue

<template>
  <div id="app">
    <div class="wrap-banner">
      <div class="button_container" @click="hello()">
        <span class="top"></span>
        <span class="middle"></span>
        <span class="bottom"></span>
      </div>
  </div>
</template>

main.js

import Vue from 'vue'
import App from './App'
import Home from './components/home'

Vue.use(Home)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  template: '<App/>',
  components: { App },
  methods: {
    hello: function() {
        alert('hello')
    }
  }
})

Screenshot

enter image description here

1
  • move methods: { hello: function() { alert('hello') } } to app.vue then try again. App.Vue is one component within #app at here because you defines template: '<App/>', components: { App }, Commented May 9, 2018 at 23:12

2 Answers 2

3

You need to move hello method to App.vue

App.vue

   <template>
  <div id="app">
   <div class="wrap-banner">
      <div class="button_container" @click="hello">
        Demo Panel
        <span class="top"></span>
        <span class="middle"></span>
        <span class="bottom"></span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {};
  },
  methods: {
    hello: function() {
      alert("hello");
    }
  }
};
</script>

main.js

import Vue from "vue";
import App from "./App";

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  template: '<App/>',
  components: { App }
})

Checkout demo at https://codesandbox.io/s/8y5yzv00nj

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

2 Comments

Hi. Thank-you I cant believe I didn't try this. My next question is why does the the <script></script> need to be with the .app.vue file. I like my javascript separate from html files?
You can check document about Vue Single File Component at vuejs.org/v2/guide/single-file-components.html As I understand, App.vue and Vue component in main.js are 2 separate component. That's why hello method is not available in App.vue in your initial code
0

Vue File Architecture

You need to be aware that a Vue file normally has 3 components (HTML, Js and CSS). This file then needs to be processed with a compiler (babel for example) in order to make it readable for the browser. See Vue Single File Components for more information.

Clean Solution

The vue-cli gives you a working starter template with babel and webpack all preconfigured. Just create a vue project and change the template as needed.

  1. Install Vue-CLI
  2. vue create my-project
  3. npm run dev

Quick Solution

If you do not want to use a compiler, just implement it like following:

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!'
  },
  methods: {
    hello: function() {
        alert('hello')
    }
  }
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <p>{{ message }}</p>
  <button @click="hello()">Click me</button>
</div>

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.