You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导入Vuex Store到Laravel页面时遇unexpected token错误求助

Fixing "Unexpected Token {" When Importing Vuex Store

Hey there! I totally get how frustrating this syntax error can be when you're just starting out with Vue and Vuex—let's get this sorted out quickly.

The root issue here is almost certainly a mismatch between how you're exporting your Vuex store and how you're importing it into your main page. Let's break down the two common scenarios and fix your code step by step:

1. Check Your Store's Export Type

First, open up your store file (usually store/index.js) and look at how you're exporting the store instance. There are two main ways to do this, and each requires a different import syntax:

Scenario A: Default Export (Most Common)

This is the standard Vuex setup. Your store file should look something like this:

// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// We use `export default` here
export default new Vuex.Store({
  state: {
    // Your state properties go here
  },
  mutations: {
    // Your mutations go here
  },
  actions: {
    // Your actions go here
  }
})

Correct Import Syntax (No Curly Braces!):
If you're using a default export, your import statement in the main page (like main.js or App.vue) should not have curly braces around store:

// main.js or App.vue
import store from './store' // No { } needed here!

Scenario B: Named Export

If you explicitly named your export (less common but valid), your store file would look like this:

// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// We use `export const store` here (named export)
export const store = new Vuex.Store({
  // Your store configuration
})

Correct Import Syntax (With Curly Braces):
For named exports, you must wrap store in curly braces when importing:

// main.js or App.vue
import { store } from './store' // Curly braces are required here

2. Why You're Seeing Those Errors

  • If you tried using import { store } from './store' but your store uses a default export, JavaScript doesn't recognize the curly braces in that context—hence the "unexpected token {" error.
  • If you removed the curly braces but your store uses a named export, JavaScript can't find a default export named store, leading to the "unexpected identifier" error.

3. Don't Forget to Inject the Store!

Once you've got the import right, make sure you inject the store into your root Vue instance so all components can access it:

// main.js
import Vue from 'vue'
import App from './App.vue'
import store from './store' // Or import { store } from './store' if using named export

new Vue({
  store, // Inject the store here
  render: h => h(App)
}).$mount('#app')

That should fix those syntax errors and get your Vuex store up and running. Let me know if you run into any other hiccups!

内容的提问来源于stack exchange,提问作者7tacos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:32:32