npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spa-custom-hook

v1.9.0

Published

业务层的自定义钩子,支持vue架构和各种小程序

Downloads

18

Readme

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