j-component: WeChat Mini Program Component Framework for Web

1.4.9 · active · verified Tue Apr 21

j-component is a JavaScript/TypeScript framework designed to enable WeChat Mini Program custom components to run within a standard web environment. It provides a mechanism to register behaviors and components, create component instances, and interact with them via a component API that closely mimics the Mini Program paradigm. The current stable version is 1.4.9. Its primary use case is facilitating the development, testing, and display of Mini Program components outside of the native WeChat environment, potentially for web-based previews or integration into web applications. Key differentiators include its explicit goal of replicating the Mini Program component system on the web, offering a parallel, independent implementation. Users should be aware that it has acknowledged performance limitations and may not support the full feature set of the native Mini Program environment, as it is not a direct subset.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a behavior, registering global and custom components, creating an instance, and interacting with its methods and data. It shows how Mini Program component logic translates to the web context.

import jComponent from 'j-component';

// Define a behavior
const myBehavior = jComponent.behavior({
  data: { behaviorText: 'Hello from behavior' },
  methods: {
    logBehavior: function() {
      console.log('Behavior method called:', this.data.behaviorText);
    }
  }
});

// Register a global component 'my-view'
jComponent.register({
  id: 'my-view',
  tagName: 'wx-view',
  template: '<div style="border: 1px solid green; padding: 10px;"><slot/></div>'
});

// Register a custom component 'my-child-component'
const childComponentId = jComponent.register({
  tagName: 'my-child',
  template: '<my-view>Child Component Content: {{text}}</my-view>',
  properties: { text: String },
  behaviors: [myBehavior],
  methods: {
    onTap: function() {
      console.log('Child tapped!');
      this.triggerLifeTime('customEvent', { detail: 'data' });
    }
  }
});

// Register a root component that uses the child component
const rootComponentId = jComponent.register({
  template: '<my-child id="myChild" text="Initial Text"></my-child>',
  usingComponents: {
    'my-child': childComponentId
  },
  ready: function() {
    const child = this.querySelector('#myChild');
    if (child) {
      child.instance.logBehavior();
      child.setData({ text: 'Updated Text from Parent' }, () => {
        console.log('Child data updated.');
      });
      // Simulate an event
      child.dispatchEvent('touchstart', { touches: [{ x: 0, y: 0 }] });
    }
  }
});

// Create an instance of the root component
const rootComponentInstance = jComponent.create(rootComponentId);

// Append the component's DOM to the document body (for browser environments)
document.body.appendChild(rootComponentInstance.dom);

console.log('Root Component JSON:', rootComponentInstance.toJSON());

view raw JSON →