
Company News
Andrew Becherer Joins Socket as Chief Information Security Officer
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.
@empjs/bridge-vue2
Advanced tools
EMP Bridge Vue2 是一个用于在 React 应用中集成 Vue2 组件的桥接工具,它解决了 React 与 Vue2 之间组件共享和通信的问题。
# 使用 npm
npm install @empjs/bridge-vue2
# 使用 yarn
yarn add @empjs/bridge-vue2
# 使用 pnpm
pnpm add @empjs/bridge-vue2
// 在 Vue2 应用中
import Vue from 'vue';
// 创建要共享的 Vue2 组件
const HelloVue = {
name: 'HelloVue',
props: {
name: {
type: String,
default: 'Vue2'
}
},
template: '<div>Hello from {{ name }}!</div>'
};
// 导出组件
export default HelloVue;
// 在 React 应用中
import React from 'react';
import { createRemoteAppComponent } from '@empjs/bridge-react';
import { createBridgeComponent } from '@empjs/bridge-vue2';
// 导入远程 Vue2 组件
import v2App from 'v2h/HelloVue';
// 获取全局 Vue 实例(通过适配器注入)
const { EMP_ADAPTER_VUE_v2 } = window;
const { Vue } = EMP_ADAPTER_VUE_v2;
// 创建 Vue2 桥接组件
const BridgeComponent = createBridgeComponent(v2App, { Vue });
// 创建可在 React 中使用的组件
export const RemoteVue2App = createRemoteAppComponent(BridgeComponent, { React });
// 在 React 应用中使用
function App() {
return (
<div>
<h1>My React App</h1>
<RemoteVue2App name="vue2 in React" />
</div>
);
}
在开发环境中,您必须在 bootstrap.ts 添加热更新支持:
// 只在热更新时加载 vue-2-hmr 模块
if (module.hot) {
console.log('vue-2-hmr', module);
import('src/adapter/vue-2-hmr');
}
vue-2-hmr: 预载组件
import 'v2h/HelloVue'
用于生产者包装 Vue2 组件。
function createBridgeComponent(
Component: any,
options: {
Vue?: any;
plugin?: (vue: any) => void;
instanceOptions?: {
store?: any;
router?: any;
[key: string]: any;
};
}
): BridgeProvider
参数:
Component: 要导出的 Vue2 组件options: Vue2 相关配置
Vue: Vue2 实例plugin: (可选) 用于扩展 Vue 功能的插件函数instanceOptions: (可选) 传递给 Vue 实例的选项
store: (可选) Vuex store 实例router: (可选) Vue Router 实例plugin 内容参考
import ElementUI from 'element-ui'
import Router from 'vue-router'
import Vuex from 'vuex'
import 'element-ui/lib/theme-chalk/index.css'
export default Vue => {
Vue.use(Router)
Vue.use(Vuex)
Vue.config.productionTip = false
Vue.use(ElementUI)
}
store 内容参考
countStore = ()=>{}是必须,避免后置请求报错
import Vuex from 'vuex'
export const countStore = () =>
new Vuex.Store({
state: {
count: 0,
},
mutations: {
increment(state) {
state.count++
},
},
})
export default countStore
用于消费者加载远程组件。
function createRemoteAppComponent(
component: ComponentProvider,
reactOptions: {
React: any;
ReactDOM?: any;
createRoot?: Function;
},
options?: {
onError?: (error: Error) => void;
}
): React.ComponentType<any>
参数:
component: 组件提供者函数,通常是 createBridgeComponent 的返回值reactOptions: 当前应用的 React 相关配置
React: React 实例ReactDOM: (可选) ReactDOM 实例createRoot: (可选) React 18+ 的 createRoot 方法options: (可选) 额外配置
onError: 错误处理回调函数FAQs
Emp Bridge Vue v2
The npm package @empjs/bridge-vue2 receives a total of 8 weekly downloads. As such, @empjs/bridge-vue2 popularity was classified as not popular.
We found that @empjs/bridge-vue2 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.

Company News
Replit is integrating Socket Firewall into its AI-powered development experience to help protect builders from malicious open source packages.

Security News
npm confirmed a tooling bug incorrectly marked several one-character packages as security holders and said it was working on a rollback.