Parsing error: Unexpected keyword 'export' in Nuxt.js

Parsing error: Unexpected keyword 'export' in Nuxt.js

在 'store' 下的 index.js 文件中,我有以下代码:

import Vuex from 'vuex'
// import axios from 'axios'

const createStore = () => {
  return new Vuex.Store({
    state: {
      loadedPosts: []
    },


//... [more code]
//... [more code]
//... [more code]
//...

export default createStore

我收到一条错误消息,指出解析错误:意外关键字 'export'

有人知道这是什么意思吗?错误发生在以下文件的最后一行:

import Vuex from 'vuex'
// import axios from 'axios'

const createStore = () => {
  return new Vuex.Store({
    state: {
      loadedPosts: []
    },


    mutations: {
      setPosts (state, posts) {
        state.loadedPosts = posts
      },
      addPost (state, post) {
        state.loadedPosts.push(post)
      },
      editPost (state, editedPost) {
        const postIndex = state.loadedPosts.findIndex(post => post.id === editedPost.id)
        state.loadedPosts[postIndex] = editedPost.id
      },
      setToken (state, token) {
        state.token = token
      },
      clearToken (state) {
        state.token = null
      }
    },


    actions: {
      nuxtServerInit (vuexContext, context) {
        return context.app.$axios
          .$get('/posts.json')
          .then((data) => {
            const postsArray = []
            for (const key in data) {
              postsArray.push({ ...data[key], id: key })
            }
            vuexContext.commit('setPosts', postsArray)
          })
          .catch(error => context.error(error))
      },

      addPost (vuexContext, post, res) {
        const createdPost = {
          ...post,
          updatedDate: new Date()
        }
        return this.$axios
          .post(process.env.baseURL + '/posts.json?auth=' + vuexContext.state.token, createdPost)
          .then((data) => {
            vuexContext.commit('addPost', { ...createdPost, id: data.name })
            this.$router.push('/admin')
          })
          // .catch(error => console.log(error))
      },

      editPost (vuexContext, editedPost) {
        return this.$axios.$put(process.env.baseURL + '/posts.json' + editedPost.id + '.json?auth=' + vuexContext.state.token, editedPost)
          .then((res) => {
            vuexContext.commit('editPost', editedPost)
          })
          // .catch(error => console.log((error)))
      },

      setPosts (vuexContext, posts) {
        vuexContext.commit('setPosts', posts)
      },

      authenticateUser (vuexContext, authData) {
        if (!authData.isLogin) {
          return this.$axios.$post('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=' + process.env.fbAPIKey, {
            email: this.email,
            password: this.password,
            returnSecureToken: true
          })
            .then(result => console.log(result))
            .catch(error => console.log(error))
        } else if (authData.isLogin) {
          return this.$axios.$post('https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=' + process.env.fbAPIKey, {
            email: authData.email,
            password: authData.password,
            returnSecureToken: true
          })
            .then((result) => {
              vuexContext.commit('setToken', result.idToken),
              localStorage.setItem('token', result.idToken),
              localStorage.setItem('tokenExpiration', new Date().getTime() + result.expiresIn * 1000)
              vuexContext.dispatch('setLogoutTimer', result.expiresIn * 1000)
            })
            .catch(error => console.log(error))
        }
      },

      setLogoutTimer (vuexContext, duration) {
          setTimeout (() => {vuexContext.commit ('clearToken')}, duration)
        },

        initAuth (vuexContext) {
          const token = localStorage.getItem('token')
          const expirationDate = localStorage.getItem('tokenExpiration')

          if (new Date().getTime() > +expirationDate || !token) {
            return
          }
          vuexContext.dispatch('setLogoutTimer', +expirationDate - new Date().getTime())
          vuexContext.commit('setToken', token)
    }
    },

    getters: {
      loadedPosts (state) {
        return state.loadedPosts
      },
      isAuthenticated (state) {
        return state.token != null
      }
    }
  })
},

export default createStore

export default createStore 发生错误,提示解析错误:意外关键字 'export'。在 export default 之前删除逗号会产生更多错误,这些错误涉及 .then(result) => 部分。如果我删除 'export default createStore.'

之前的逗号,它告诉我 localStorage.setItem 是 'expecting assignment or function calls instead of expressions'

您必须删除 export 之前的 , 并将数据分配给您的 localStorage 项目,如下所示:

 localStorage['tokenExpiration']= new Date().getTime() + result.expiresIn * 1000

我在 export 之前删除了 ,,它起作用了!谢谢你,Boussadjra Brahim :D

这就是我的代码现在的样子:

 // some.js

 const title = [
{...},
{...},
{...},
]
        
export { title };