Detailed explanation of Vuex environment

Detailed explanation of Vuex environment

Build Vuex environment

Create a folder store in the src directory and create an index.js file in the store folder

index.js is used to create the core store in Vuex

// scr/vuex/index.js
 // Import Vuex
import Vuex from 'vuex'
 // Used to respond to actions in components const actions = {}
// Used to manipulate data const mutations = {}
// Used to store data const state = {}
 // Create a store
const store = new Vuex.Store({
    actions,
    mutations,
    state
})
 // Export store
export default store
// main.js
import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'
import store from './store/index'
 Vue.use(Vuex)
 new Vue({
    render:h => h(App),
    store
}).$mount('#app')

But this will result in an error:

[vuex] must call Vue.use(Vuex) before creating a store instance

Meaning : [vuex] Vue.use(Vuex) must be called before creating a store instance

Reason : When we import the store, the code of the imported file is executed first, so when the following code is executed, the imported file has been executed

In this case, let's swap the two lines of code: import store from './store/index' and Vue.use(Vuex)

But the actual result is: [vuex] must call Vue.use(Vuex) before creating a store instance , still error

Reason : This is a problem with the scaffold parsing import statements. The imported file will be placed at the beginning of the code, and then the code of this file will be parsed.

Correct way to write it:

// scr/store/index.js
 // Import Vuex and Vue
import Vuex from 'vuex'
import Vue from 'vue'
 // Apply Vuex plugin Vue.use(Vuex)
 // Used to respond to actions in components const actions = {}
// Used to manipulate data const mutations = {}
// Used to store data const state = {}
 // Create a store
const store = new Vuex.Store({
    actions,
    mutations,
    state
})
 // Export store
export default store
// main.js
import Vue from 'vue'
import App from './App.vue'
import store from './store/index'
 new Vue({
    render:h => h(App),
    store
}).$mount('#app')

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Vue Vuex builds vuex environment and vuex sum case sharing
  • Vue Beginner's Guide: Environment Building and Getting Started
  • Detailed summary of Vue project environment construction
  • Build a Vue development environment
  • Simple tutorial on setting up vue environment
  • Vue2.0 from scratch_Environment construction steps
  • Detailed explanation of the simplest strategy for setting up a vue.js development environment
  • Step-by-step guide to build and install a Windows-based Vue.js runtime environment
  • Tutorial on setting up vue.js development environment
  • Vue project environment construction

<<:  Parsing Linux source code epoll

>>:  Detailed explanation of the use of base tag in HTML

Recommend

CSS transparent border background-clip magic

This article mainly introduces the wonderful use ...

IDEA complete code to connect to MySQL database and perform query operations

1. Write a Mysql link setting page first package ...

How to move mysql5.7.19 data storage location in Centos7

Scenario: As the amount of data increases, the di...

How to implement JavaScript output of Fibonacci sequence

Table of contents topic analyze Basic solution Ba...

Detailed explanation of Js class construction and inheritance cases

The definition and inheritance of classes in JS a...

Detailed explanation of meta tags (the role of meta tags)

No matter how wonderful your personal website is,...

How to prevent Flash from covering HTML div elements

Today when I was writing a flash advertising code,...

Native JS to achieve special effects message box

This article shares with you a special effect mes...

How to understand JS function anti-shake and function throttling

Table of contents Overview 1. Function debounce 2...

How to construct a table index in MySQL

Table of contents Supports multiple types of filt...

Introduction to possible problems after installing Tomcat

1. Tomcat service is not open Enter localhost:808...

How to use custom images in Html to display checkboxes

If you need to use an image to implement the use ...

Tutorial on installing GreasyFork js script on mobile phone

Table of contents Preface 1. Iceraven Browser (Fi...