簡體   English   中英

在.vue組件中編譯方法時出現語法錯誤

[英]syntax error when compiling method in .vue component

我正在按照教程( https://savvyapps.com/blog/definitive-guide-building-web-app-vuejs-firebase )構建一個vue.js應用程序,並在指南的其中一個部分中指導我在Login.vue文件的塊中添加login()函數。

    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

我一直在努力弄清楚語法錯誤是什么,但無濟於事。 先謝謝您的幫助!

login()屬於組件的methods對象:

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

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM