j-component: WeChat Mini Program Component Framework for Web
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
-
TypeError: jComponent.register is not a function
cause The jComponent object was not imported correctly, or the module was not bundled/loaded as expected in the environment.fixEnsure `import jComponent from 'j-component';` is at the top of your file. If using CommonJS, `const jComponent = require('j-component');` must be used. Verify your bundler (e.g., Webpack, Rollup) is correctly processing the module. -
Failed to resolve component 'my-child'. Please check 'usingComponents' configuration.
cause A component used in a template was not declared in the `usingComponents` object of the parent component, or the ID/path was incorrect.fixEnsure that every custom component used within a template is explicitly listed in the `usingComponents` object with its correct `id` (if globally registered) or `componentId` (returned from `jComponent.register`). -
WXML template parsing error: Invalid character in tag name.
cause The `template` string provided to `jComponent.register` contains malformed WXML (or HTML) syntax, such as invalid tag names or unclosed tags.fixReview the `template` string for syntax errors. Ensure it adheres to valid WXML/HTML structure. Pay attention to custom component tags and ensure they are correctly formed.
Warnings
- gotcha The framework's performance is not equivalent to the native WeChat Mini Program implementation. It may exhibit slower rendering or update speeds.
- gotcha j-component does not implement the full feature set of the native WeChat Mini Program. Some advanced or niche Mini Program features might be absent or behave differently.
- gotcha j-component is an independent implementation and not a direct subset or equivalent of the native Mini Program. Do not assume 100% compatibility or identical behavior across all scenarios.
Install
-
npm install j-component -
yarn add j-component -
pnpm add j-component
Imports
- jComponent
const jComponent = require('j-component');import jComponent from 'j-component';
- Component
import type { Component } from 'j-component'; - BehaviorDefinition
import type { BehaviorDefinition } from 'j-component';
Quickstart
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());