Getting Started - Raiondesu/Tuex GitHub Wiki
At the center of every Vuex application is the store. A "store" is basically a container that holds your application state. There are two things that make a Vuex store different from a plain global object:
- Vuex stores are reactive. When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes.
- You cannot directly mutate the store's state. The only way to change a store's state is by explicitly committing mutations. This ensures every state change leaves a track-able record, and enables tooling that helps us better understand our applications.
Vuex documentation
Same thing is with Tuex. Except that you can (and you should) mutate the state directly. It's also possible to choose which store actions leave records and which do not. This is especially useful when things in need of tracking are outside of the setters (mutation handlers). The Vuex-like store behaviour is easily simulated within Tuex with the help of the strict flag.
The Simplest Store
NOTE: We will be using ES2015 syntax for code examples for the rest of the docs. If you haven't picked it up, you should!
After installing Tuex, let's create a store. It is pretty straightforward - just provide an initial store object or a constructor function:
// Make sure to call Vue.use(Tuex) first
const tuex = new Tuex.Store({
num: 0,
increment() {
this.num++;
}
});
// OR
const tuex = new Tuex.Store(class {
constructor() {
this.num = 0;
}
increment() {
this.num++;
}
});
It's that simple!
Now, you can access the proposed object as tuex.store
, and trigger the store change... however the hell you want!
tuex.store.num = -2;
console.log(tuex.store.num);
// => -2
tuex.store.increment();
console.log(tuex.store.num);
// => -1
Here, in both cases, Tuex tracked the mutation of a variable num
! What a miracle!
The cool thing with Tuex is that, again, it doesn't force you to follow any pattern.
You do the pattern. Tuex does the tracking.
You still have the ability to create logging tools and etc. Only in this case you can actually decide what to log and what to track.
Next, we will discuss each core concept in much finer details.