1.9.0 • Published 2 years ago

spa-custom-hook v1.9.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

license license license  license license

Simplified Chinese | English

What is spa-custom-hooks?

  • A thing that can customize page hooks, you can register global asynchronous tasks, define the triggering conditions of the hooks yourself, and automatically execute the relevant hooks in the page when the conditions are met.
  • Supports the use of created, mounted, etc. with vue's native hooks.
  • Support vue architecture (including uni-app, wepy, mpvue, etc.) and various small programs.

What is it for?

Solve the problem of page logic relying on global asynchronous data in a simple and elegant way

Common application scenarios

export default {
    name:'Home',
    onCreatedLogin(){
        //Successful login (get the token) && page initialization completed
        //Tips: Suitable for scenarios where the request sent by a page depends on the token
    },
    onCreatedUserInfo(){
        //Page initialization is complete && Obtaining user information is complete
        //Tips: Suitable for scenarios where user information needs to be used to make judgments when the page is initialized, and then go to the page logic
    },
    onMountedUserInfo(){
        //dom rendering completed && access user information completed
        //Tips: Suitable for similar scenes where the avatar needs to be rendered on the canvas when entering the page for the first time
    },
    onReadyShow(){
        //The page rendering in the applet is completed && page display
        //Tips: Suitable for scenarios where you need to obtain small program components or dom, and will be executed every time the page is displayed
    },
}

Usage example

Click to view the snippet of applet code

//The first step is to install the plug-in:
npm install spa-custom-hooks

//The second step is to register the plug-in in the entry file:
import CustomHook from'spa-custom-hooks';
const diyHooks = {
     'UserInfo':{
        name:'UserInfo',
        watchKey:'userinfo',
        deep: true,
        onUpdate(val){
            // If the nickName in userinfo is not empty, it means that the hook is hit
            return !!val.nickName;
        }
    }
}
//1. Registration method of vue architecture
import store from'./store'
Vue.use(CustomHook ,diyHooks,store)
//2. Registration method of native applet
//Define globalData in advance
const globalData = {
    userinfo: {
        nickName:''
    }
}
CustomHook.install(diyHooks,globalData)

//The third step is to use plug-ins in the business page (any page can be used, with low coupling and less repetitive code):
onLoadUserInfo(){
    //Can render canvas
    renderCanvas();
}

Registration parameter description

Sign up for CustomHook

//vue architecture-main.js
import store from'./store'
import CustomHook from'spa-custom-hooks';
Vue.use(CustomHook,diyHooks,store)
//Native applet architecture-app.js
import CustomHook from'spa-custom-hooks';
CustomHook.install(diyHooks,globalData)

diyHooks object description

{
    //1. Register the attribute monitoring hook
    //UserInfo, hook single name, initial letter capitalized
    'UserInfo':{
        //name, the full name of the hook, it can be the same as the key above if the monitoring attribute is required, it is required
        name:'UserInfo',
        //The attribute name in the store to be monitored by watchKey (equivalent to $store.state.userinfo), the attribute monitoring hook mode is required
        watchKey:'userinfo',
        //Whether to hit by default, not required
        hit: false,
        //deep Whether to monitor deeply, not required
        deep: true,
        //The callback executed when the onUpdate property is changed is used to determine whether to hit this hook. It is not required. The default value is equivalent to returning!! val
        onUpdate(val){
            //This means that userinfo contains nickName and hits this hook. Note that you cannot return asynchronously
            return !!val.nickName;
        }
    },
    
    //2. Register event listener hook
    //BeforeMount, hook single name, first letter capitalized
    'BeforeMount':{
        //name, the name of the native hook, used to hit this hook, required
        name:'beforeMount',
        //destroy, the opposite hook name, used to cancel the hit, the event listener hook is required
        destroy:'destroyed',
        //Whether to hit by default, not required
        hit: false
    }
}

Hook usage rules

`on{UserInfo}{BeforeMount}{Login}{Position}...` //All registered hooks can be matched at will, the arrangement order does not affect the execution of the hooks, they are all in && relationship

Registered life cycle hooks

Launch, Created, Load, Attached, Show, Mounted, Ready
//↓↓↓If you need other hooks, you can register by yourself↓↓↓(If a hook of the current framework and its corresponding opposite hook are inconsistent with the following configuration, you also need to manually register, for example, wepy has created but not destroyed)

Demo QR code

left image description here

Join group communication

left image description here

If you have any good suggestions, please raise issues or pr

Click a star if you like

1.9.0

2 years ago

1.8.0

2 years ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago