Vuet
The index
- What is Vuet.js?
- Differences between 0.x and 1.x versions
- The installation
- Quick start
- API
- Instance options
- Instance properties
- Method of instance
- Global API
- The module
- What is a module?
- Registration module
- Connect modules using compute properties
- Gets the routing object in the module
- Resetting module status
- The rules
- What are the rules?
- Built-in rules
- Custom rules
- Third-party plug-ins
- Third Party projects
What is Vuet.js?
The only way to update the state in VUEX is through mutation, which is trivial, whereas in Vuet it is pleasant to update directly when and where assignment is allowed. Vuet is a centralized state management mode, which provides a module system and a rule system. It exists for the purpose of making state management easy
In the 0.x release, we built in so much functionality that we raised the bar for entry significantly, while in the 1.x release, we simplified it and kept only a few core apis. Note: Route rule has been deleted from Vuet, and will be released in the form of plug-in, please look forward to it! 0. X version address
The installation
npm install vuet@latestCopy the code
import Vue from 'vue'
import Vuet, { mapModules.mapRules } from 'vuet'
Vue.use(Vuet)
const vuet = new Vuet({
//Examples of options, details below
})
vuet.addModules('test', {
data () {
return {
count: 0}},fetch () {
this.count = 1000
},
plus () {
this.count++
},
reduce () {
this.count--
},
modules: {
//You can add a submodule, which automatically inherits the name of the parent module: path = Parent module name/submodule name}})const App = {
mixins: [
mapModules({
test: 'test' //{alias: 'module path'}
}),
mapRules({
once: [{ path: 'test' }] //{rule: [' module path ']} The built-in rules and abbreviations can be seen below
})
],
template: `
<div>
<div class="count">{{ test.count }}</div>
<button @click="test.plus">plus</button>
<button @click="test.reduce">reduce</button>
<button @click="test.reset">reset</button>
<button @click="test.fetch">fetch</button>
</div>
`
}
export default new Vue({
el: '#app',
vuet,
render (h) {
return h(App)
}
})
Copy the code
API
options.pathJoin
- Description: Separator when a child module inherits a parent module
- Default value:
/
options.modules
- Description: The module to initialize
- Default value:
{}
Instance properties
vuet.app
- Description:
new Vuet({ vuet })
At the time of theVue
The instance - Default value:
null
vuet.modules
- Description: Added modules, all here
- Default value:
{}
vuet.options
- Description:
new Vuet(options)
Parameters passed in when instantiated - Default value:
{ pathJoin: '/', modules: {} }
vuet.store
- Description: The state stored by each module
- Default value:
{}
vuet.vm
- Description: Vue instance inside vuET
vuet.addModules(path: string, modules: Object)
- Description: Registration module
vuet.getModule(path: string)
- Description: Returns the status and methods of the module
vuet.getState(path: string)
- Description: Only module status is returned, no method is returned
vuet.destroy()
- Description: Destroy program, free memory. When a VUE instance is destroyed, it is also automatically destroyed
Global API
Vuet.mapmodules (opts: {alias: 'module path'})
- Description: Connect modules in Vue components so that methods and states of modules can be used in components
Vuet.maprules (opts: {rule: [{path: 'module path}]})
- Description: Use the corresponding rules to update the module. Supported abbreviations:
{rule: 'module path'}
,{rule: [' module path ']}
Vuet.rule(name: string, opts: Object)
- Description: Register global rules. For details, see the following custom rules
What is a module?
Module is like the basic skeleton of a person, the attributes of the module are like the hands, feet, eyes and so on, and the method of the module is like the brain, which controls the behavior of people, such as the code by hand, walking with feet, when seeing beautiful women blink
const vuet = new Vuet(a)//Registered a module called test
vuet.addModules('test', {
data () {
return {
count: 0 //Defines a count attribute}},plus () { //Defines a plus method
this.count++
},
modules: { //Defining submodules
chlid: { //Submodule path = Parent module/submodule = test/chlid
data () {
return {
count: 0 //Defines a count attribute}},plus () { //Defines a plus method
this.count++}}}})Copy the code
Connect modules using compute properties
{
computed: {
test () { //While it is possible to inject modules into components using the mapModules method, it is also possible to obtain modules by evaluating properties
return this.$vuet.getModule('The module path')}},beforeCreate () {
//This. test gets the module and can call its methods and properties
//This.$vuet gets the instance of the vuet and can play happily}}Copy the code
const vuet = new Vuet(a)vuet.addModules('test', {
data () {
return{}},plus () {
this.vuet //Get the VUET instance
this.app //Get the vue root instance
this.app.$router //Method to obtain a route
this.app.$route //Gets the status of the route}})Copy the code
Resetting module status
const vuet = new Vuet(a)vuet.addModules('test', {
data () {
return {
count: 0}},plus () {
this.count = 100 //As this. The state. The count
setTimeout(() = > {
this.reset(a)//This is a vuet built-in reset method equivalent to this.state = this.data().
}, 2000)}})Copy the code
What are the rules?
In Vuet, rules are a special presence in Vuet that allows you to abstract away the similar process of updating the state of a module. You can use rules to update any module. You can think of a Vuet module as a car, and the rules define how the car should go, should it go straight, should it turn, should it go straight for a while, and then turn, all defined by rules
need
- Description: Each time the component is initialized, the
beforeCreate
Hookfetch
methods
once
- Description: Only the first time in the component
beforeCreate
Hookfetch
Method, and no further updates are made in any component
temp
- Description: When the component is initialized, the
beforeCreate
Hookfetch
Method when the component is destroyed indestroyed
Resets module state in the hook
Custom rules
The main principle is to get the module path passed in, and return a mixin injected into the component. We are now trying to define a drag process that calls the fetch method of the module every time the component executes the beforeCreate hook. We are now trying to define a myRule
Vuet.rule('myRule', { //Note: Registration of rules must precede execution of all components
install (Vuet.Vue) {
//Pass in a Vuet and Vue constructor. It will only be called once
},
init (vuet) {
//After new Vuet() is instantiated, the instance is passed in, where you can add modules, methods and so on. Each new Vuet instance executes a hook
},
addModule (vuet.path) {
//New Vuet().addModules Each time a module is registered, the hook is executed
}
rule ({ path }) {
//Pass in the path to the current module and return a mixin to inject into the component. Called when Vuet's mapRules method is executed
return {
beforeCreate () {
//Get the current module with the path
const vtm = this.$vuet.getModule(path)
//The fetch method of the module is then called once
vtm.fetch()}}},destroy (vuet) {
//Pass in the vuET instance you are currently destroying, and you can do something with it yourself}})Copy the code
Inject rules to update modules into components
{
mixins: [
mapRules({
'myRule': 'Updated module path'})]//. options
}Copy the code
- Vuet-scroll Records the scroll bar position
- vuet-routeAccording to the
vue-router
To rerequest the data
Third Party projects
- Vue-cnode Cnode community implemented by VUE + Vuet
MIT