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

vue-create-api

v0.2.3

Published

A Vue plugin which make Vue component invocated by API.

Downloads

566

Readme

vue-create-api

A Vue plugin which make Vue component invocated by API.

中文文档

Installing

use npm

$ npm install vue-create-api

use cdn

<script src="https://unpkg.com/vue-create-api/dist/vue-create-api.min.js"></script>

Usage

import CreateAPI from 'vue-create-api'

Vue.use(CreateAPI)

// or with options.

Vue.use(CreateAPI, {
  componentPrefix: 'cube-'
  apiPrefix: '$create-'
})

// then the Vue constructor will have the createAPI function.

import Dialog from './components/dialog.vue'

// make Dialog component invocated by API.

Vue.createAPI(Dialog, true)

// use in general JS files.
// however, the $props can not be reactive.

Dialog.$create({
  $props: {
    title: 'Hello',
    content: 'I am from pure JS'
  }
}).show()

// use in a vue component.

this.$createDialog({
  $props: {
    title: 'Hello',
    content: 'I am from a vue component'
  },
}).show()
// typescript
import CreateAPI from 'vue-create-api'

Vue.use(CreateAPI)

Vue.createAPI(Dialog, events, single)

this.$createDialog({
  $props: {
    title: 'Hello',
    content: 'I am from a vue component'
  }
}).show()
// d.ts
import Vue, { VueConstructor } from 'vue'
import { createFunction } from 'vue-create-api';

export declare class UIComponent extends Vue {
  show ():void
  hide ():void
}

declare module 'vue/types/vue' {
  interface Vue {
    /** create Dialog instance */
    $createDialog: createFunction<UIComponent>
  }
}

Tip

using typescript, terser-webpack-plugin(vue-cli3.x) or uglifyjs(vue-cli2.x) adds { keep_fnames: true }

Constructor Options

|key|description|default| |:---|---|---| | componentPrefix|the prefix name of your component| - | |apiPrefix|the api prefix|$create|

Methods

Vue.createAPI(Component, [single])

  • Parameters:

    • {Function | Object} Component Vue component which must contains name
    • {Boolean} [single] whether singleton
  • Usage:

    • This method will add a method which is named $create{camelize(Component.name)} to Vue's prototype, so you can instantiate the Vue component by const instance = this.$createAaBb(config, [renderFn, single]) in other components. The instantiated component's template content will be attached to body element.

    • const instance = this.$createAaBb(config, renderFn, single)

      Parameters:

      | Attribute | Description | Type | Accepted Values | Default | | - | - | - | - | - | | config | Config options | Object | {} | - | | renderFn | Optional, used to generate the VNode child node in the slot scene in general | Function | - | function (createElement) {...} | | single | Optional, whether the instantiated component is a singleton or not. If two parameters are provided and the renderFn's type is not function, then the single value is the sencond parameter's value. | Boolean | true/false | single in createAPI() |

      Config options config:

      You can set $props and $events in config, $props supported reactive properties, these props will be watched.

      | Attribute | Description | Type | Accepted Values | Default | | - | - | - | - | - | | $props | Component props | Object | - | { title: 'title', content: 'my content', open: false} | | $events | Component event handlers | Object | - | { click: 'clickHandler', select: this.selectHandler} |

      $props example, { [key]: [propKey] }:

      {
        title: 'title',
        content: 'my content',
        open: false
      }

      title, content and open are keys of the component prop or data, and the prop' value will be taken by the following steps:

      1. If propKey is not a string value, then use propKey as the prop value.
      2. If propKey is a string value and the caller instance dont have the propKey property, then use propKey as the prop value.
      3. If propKey is a string value and the caller instance have the propKey property, then use the caller's propKey property value as the prop value. And the prop value will be reactive.

      $events example, { [eventName]: [eventValue] }:

      {
        click: 'clickHandler',
        select: this.selectHandler
      }

      click and select are event names, and the event handlers will be taken by the following steps:

      1. If eventValue is not a string value, then use eventValue as the event handler.
      2. If eventValue is a string value, then use the caller's eventValue property value as the event handler.

      You can set all avaliable properties in Vue, but you need to add prefix $, eg:

      this.$createAaBb({
        $attrs: {
          id: 'id'
        },
        $class: {
          'my-class': true
        }
      })

      The Returned value instance:

      instance is a instantiated Vue component.

      And the remove method will be attached to this instance.

      You can invoke the remove method to destroy the component and detach the component's content from body element.

      If the caller is destroyed and the instance will be automatically destroyed.

  • Example:

    First we create Hello.vue component:

    <template>
      <div @click="clickHandler">
        {{content}}
        <slot name="other"></slot>
      </div>
    </template>
    
    <script type="text/ecmascript-6">
      export default {
        name: 'hello',
        props: {
          content: {
            type: String,
            default: 'Hello'
          }
        },
        methods: {
          clickHandler(e) {
            this.$emit('click', e)
          }
        }
      }
    </script>

    Then we make Hello.vue as an API style component by calling the createAPI method.

      import Vue from 'vue'
      import Hello from './Hello.vue'
      import CreateAPI from 'vue-create-api'
      Vue.use(CreateAPI)
    
      // create this.$createHello API
      Vue.createAPI(Hello, true)
    
      // init Vue
      new Vue({
        el: '#app',
        render: function (h) {
          return h('button', {
            on: {
              click: this.showHello
            }
          }, ['Show Hello'])
        },
        methods: {
          showHello() {
            const instance = this.$createHello({
              $props: {
                content: 'My Hello Content',
              },
              $events: {
                click() {
                  console.log('Hello component clicked.')
                  instance.remove()
                }
              }
            }, /* renderFn */ (createElement) => {
              return [
                createElement('p', {
                  slot: 'other'
                }, 'other content')
              ]
            })
          }
        }
      })

    In this example, we create a component Hello which needs to be invoked in api form and we invoke it in another component.The focus is what showHello() does: invoking method this.$createHello(config, renderFn) to instantiate Hello.

How to use in general JS files or use it in global

In vue component, you can call by this.$createHello(config, renderFn) because the this is just a Vue instance. But in general JS files, you need to use Hello.$create. As shown below:

import Vue from 'vue'
import Hello from './Hello.vue'
import CreateAPI from 'vue-create-api'
Vue.use(CreateAPI)

// create this.$createHello and Hello.create API
Vue.createAPI(Hello, true)

Hello.$create(config, renderFn)

Notice, when we use in general JS files, we can't make props be reactive.