简体   繁体   English

我如何从 vanilla JS 访问/改变 Vue 组件属性

[英]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 外部的组件数据交互(参见注释AB )。

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.

相关问题 如何从普通的 DOM 访问 Vue 组件 - How to access Vue Component from plain vanilla DOM 如何将 vanilla .js 添加到自定义 Vue 组件 - How to add vanilla .js to a custom Vue component 如何从Vue.js中的子组件访问父方法? - How can I access a parent method from child component in Vue.js? Vue JS:如何从 vue 组件访问和更改 Css 根变量以切换 CSS 变量站点主题? - Vue JS : How can I access and change Css root variables from vue component to toggle CSS Variables Site Theming? 如何从标准 JS 访问 Vue 组件? - How to access Vue component from standard JS? 如何从 Vue 商店中的 localStorage 读取和改变状态 - How can I read from localStorage and mutate state in Vue store 是否可以从 vue.js 中任意嵌套的子组件中更改属性,而在整个层次结构中没有事件链? - Is it possible to mutate properties from an arbitrarily nested child component in vue.js without having a chain of events in the entire hierarchy? 我如何将属性传递给Vue Js中由路由器调用的组件 - How can I pass properties to a component which is called by router in Vue Js Vue.js - 无法从方法访问计算属性 - Vue.js - Can't access computed properties from methods 在 Vue.js 中,如何通过 vanilla js 更改 DOM 后恢复数据绑定? - In Vue.js, how can I restore data binding after changing DOM through vanilla js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM