【问题标题】:syntax error when compiling method in .vue component在 .vue 组件中编译方法时出现语法错误
【发布时间】:2019-02-18 17:13:50
【问题描述】:

我正在按照教程 (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

我一直在尽力弄清楚语法错误是什么,但无济于事。提前感谢您的帮助!

【问题讨论】:

    标签: javascript vue.js webpack vuejs2 vue-component


    【解决方案1】:

    login()属于组件的methods对象:

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

    【讨论】:

      【解决方案2】:

      在您的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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 2016-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多