1

I'm having an issue with Vue. At this point I've read things like this detailing this error occurring when you try to define a method on the root instance, then reference it in local scope.

My issue is slightly different because it is defined in local scope, so I'm not sure what to make of this error. I've also looked at this, and this.

Here is my App.vue:

<template>
  <div id="app">
    <router-link to="/Home">home</router-link>
    <router-link to="/Login">login</router-link>
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App'
}

console.log('app.vue loading')
</script>

My main.js:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import store from './store'
import router from './router'
import Home from './components/Home'
import Login from './components/Login'

Vue.config.productionTip = false

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

console.log('main js loading');

The component the issue is coming from:

<template>
  <div class="login">
    <head>
        <title>{{title}}</title>
    </head>
    <form for="login" id="loginMain">
      <label for="username">Username:</label>
      <input type="text" id="username"></input>
      <label for="password">Password:</label>
      <input type="password"></input><br/><br/>
      <button for="login" @click="processLogin">LOGIN</button>
      <button for="logout" @click="processLogout">LOGOUT</button>

    </form>
    <p> Your login status: {{ loginStatus }} </login></p>
  </div>
</template>

<script>
import Vue from 'vue'
import { mapGetters, mapActions, Vuex } from 'vuex'
import store from '@/store'

const Login = {
  delimiters: [ '[{','}]'],
  data () {
    title: 'Foo',
    msg: 'Bar'
  }
  name: 'Login',
  props: {
    // worry about this later
  },
  methods: {
    ...mapActions({
      processLogin : 'LOGIN_ACTION',
      processLogout : 'LOGOUT_ACTION'
    })
  },
  computed: {
    ...mapGetters({
    title: 'GET_TITLE',
    loginStatus: 'GET_LOGIN_STATUS'
  }),
 }
}

console.log('Login loading');

export default Login

And although I'm unsure if it is related, but my store:

import Vue from 'vue'
import Vuex from 'vuex'
import Home from './components/Home'
import Login from './components/Login'

Vue.use(Vuex)

const store =  new Vuex.Store({
  state: {
    title: 'My Custom Title',
    loggedIn: false
  },
  mutations: {
    MUT_LOGIN: (state) => {
      state.loggedIn = true
    },
    MUT_LOGOUT: (state) => {
      state.loggedIn = false
    }
  },
  actions: {
   LOGIN_ACTION ({ commit }){
     store.commit('MUT_LOGIN')
   },
   LOGOUT_ACTION ({ commit, state }) {
     store.commit('MUT_LOGOUT')
   }
  },
  getters: {
    GET_LOGIN_STATUS: state => {
      return state.loggedIn
    },
    GET_TITLE: state => {
      return state.title
    }
  }
})

console.log('store loading');

export default store

I know that I had some of these errors at one point and got rid of them by revisiting each import statement and making some corrections to how I had things hooked up. The only other difference between now and then is that I am running all of these files through Webpack and serving them with Django in a template. (The reason for all the consoles, making sure all the files got in there via Webpack.)

The image below is a couple of the specific things it is barking about.

enter image description here

Because of the link from the devtools error, I have looked at this and also experimented with adding local data () properties to the component itself.

tony19
  • 125,647
  • 18
  • 229
  • 307
JTK
  • 101
  • 3
  • 14

1 Answers1

4

Changing

data () {
  title: 'Foo',
  msg: 'Bar',
},

to

data () {
  return {
    title: 'Foo',
    msg: 'Bar',
  }
},

will fix the "title" error.

As for the issue with the actions nothing is jumping out at me. Can you try mapGetters and see if you have access to the getters?

Bill Criswell
  • 32,161
  • 7
  • 75
  • 66
  • Thank you for taking a look at this - sorry, caveat - I had found that advice at some point and implemented it but made a new branch where that hadn't been done yet. I have implemented the change from data () { // stuff } to data () { return { // stuff }} and it is actually still giving me the same behavior – JTK Jun 29 '18 at 14:55
  • Also, from the Vuex console it appears as if my getters are being recognized. And the current state values for loggedIn and title are also apparent – JTK Jun 29 '18 at 14:59
  • Are they making it into the component properly though? – Bill Criswell Jun 29 '18 at 16:29
  • @JTK the code you've shared in your question wouldn't compile as it has invalid javascript. If you want help debugging your issue, please edit your code to provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) of the issue you're describing. – thanksd Jun 29 '18 at 17:05