繁体   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