[英]How can I access/mutate Vue component properties from vanilla JS
I have a Vue 2 project made with Vue CLI, and I plan to distribute it as a library, ideally with the dependencies and Vue syntax stuff abstracted away by some kind of wrapper script.我有一个使用 Vue CLI 制作的 Vue 2 项目,我计划将其作为一个库进行分发,理想情况下是通过某种包装脚本抽象出依赖项和 Vue 语法。 I would like to allow this kind of interaction:
我想允许这种互动:
// mount the component on a plain JS webpage
const myComponent = new MyComponent('#my-component');
// handle events from the component in vanilla JS
myComponent.on('load', someHandler);
// (A.) call a component method and get a return value
const processedData = myComponent.process(123);
// (B.) access/mutate reactive component data properties
myComponent.setMessage('Hello world!');
I have tried changing the "build target" to build a Libary or a Web Component as mentioned in the Vue documentation .如Vue 文档中所述,我已尝试更改“构建目标”以构建库或 Web 组件。 I can mount the library component just fine, and handle events, but it doesn't mention how I might interact with the component data from outside the Vue VM (see comments A and B ).
我可以很好地挂载库组件并处理事件,但它没有提到我如何与 Vue VM 外部的组件数据交互(参见注释A和B )。
How can I access Vue component methods and data properties from outside the Vue VM, in vanilla JS?如何在普通 JS 中从 Vue VM 外部访问 Vue 组件方法和数据属性?
To access the Vue component properties (and methods) outside of the VM, you can mount it with a " template ref " like this:要在 VM 外部访问 Vue 组件属性(和方法),您可以使用“模板引用”来挂载它,如下所示:
const vm = new Vue({
components: {
MyComponent,
},
template: `
<my-component
ref="myComponent"
/>
`,
}).$mount('#mount-element");
and then you can call its methods like this:然后你可以这样调用它的方法:
vm.$refs.myComponent.someFunction();
You'll get the returned values and it will access/mutate reactive properties inside the VM as expected.您将获得返回值,并且它将按预期访问/改变 VM 内的反应属性。
To use the class syntax described in the original question, we can create a simple class to wrap the vue component:要使用原始问题中描述的 class 语法,我们可以创建一个简单的 class 来包装 vue 组件:
// import the component built by Vue CLI with the "library" build target
// (puts `MyComponent` in the global namespace)
import './MyComponent.umd.min.js';
import Vue from 'https://unpkg.com/vue@2/dist/vue.esm.browser.min.js';
export default class {
constructor(mountElement) {
// mount vue VM with a template ref to access its properties
const thisClass = this;
this.vm = new Vue({
components: {
MyComponent,
},
template: `
<my-component
ref="myComponent"
/>
`,
}).$mount(mountElement);
this.component = this.vm.$refs.myComponent;
}
// define methods that could call this.component's functions
someFunction() {
// do stuff
return this.component.someFunction()
}
}
It seems to work pretty well.它似乎工作得很好。 A possible improvement would be to build the component library with a different tool, since Vue CLI v3 (with Vue v2 projects) can't output ESM module files, so the best we can do is a UMD modle that gets defined globally.
一个可能的改进是使用不同的工具构建组件库,因为 Vue CLI v3(带有 Vue v2 项目)不能 output ESM 模块文件,所以我们能做的最好的是一个全局定义的 UMD 模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.