синтаксическая ошибка при компиляции метода в компоненте .vue - PullRequest
0 голосов
/ 14 сентября 2018

Я следую учебному пособию (https://savvyapps.com/blog/definitive-guide-building-web-app-vuejs-firebase) для создания приложения vue.js, и в одном из разделов руководства мне было предложено добавить функцию login () в блок файла Login.vue .

    login() {
      fb.auth.signInWithEmailAndPassword(this.loginForm.email, this.loginForm.password).then(user => {
        this.$store.commit('setCurrentUser', user)
        this.$store.dispatch('fetchUserProfile')
        this.$router.push('/dashboard')
      }).catch(err => {
        console.log(err)
      }) 
    }

Однако, когда он компилируется, я получаю ошибку:

    ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/Login.vue
Module build failed: SyntaxError: C:/Users/Rohit/Documents/Javascript/vue_practice/humanity/src/components/Login.vue: Unexpected token, expected ; (33:8)

  31 | const fb = require('../firebaseConfig.js')
  32 | 
> 33 | login() {
     |         ^
  34 |     fb.auth.signInWithEmailAndPassword(this.loginForm.email, this.loginForm.password).then(user => {
  35 |         this.$store.commit('setCurrentUser', user)
  36 |         this.$store.dispatch('fetchUserProfile')

 @ ./src/components/Login.vue 4:0-105 5:0-118
 @ ./src/router/index.js
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js

Я старался изо всех сил выяснить, что такое синтаксическая ошибка, но безрезультатно. Заранее спасибо за помощь!

Ответы [ 2 ]

0 голосов
/ 14 сентября 2018

В вашем script разделе вы должны иметь следующую структуру:

<script>
const fb = require('../firebaseConfig.js')
export default{
 data(){
     return { ... };
  },
methods:{
   ...
    login() {
      fb.auth.signInWithEmailAndPassword(this.loginForm.email, 
       this.loginForm.password).then(user => {
        this.$store.commit('setCurrentUser', user)
       this.$store.dispatch('fetchUserProfile')
       this.$router.push('/dashboard')
     }).catch(err => {
    console.log(err)
     }) 
    }
  ...
   }
}
</script>
0 голосов
/ 14 сентября 2018

login() принадлежит объекту methods компонента:

export default {
  methods: {
    login() {
      ...
    }
  }
}
...