【问题标题】:How do you refer to a VueJS component's name within the template's raw HTML / x-referenced ID element?您如何在模板的原始 HTML / x-referenced ID 元素中引用 VueJS 组件的名称?
【发布时间】:2017-05-01 07:10:51
【问题描述】:

我实际上是在尝试根据注册的component-name 向我的 VueJS 组件添加一个 CSS 类(以使所有这些特定类型的组件具有相同的样式)。

例如:

Vue.component('dragdropfile', {
    // This refers to a unique template element (see HTML below)
    template: "#dragdropfile-tmp",
    props: ['action']
});

而在Vue组件模板中:

<template id="dragdropfile-tmp">
    <form action="{{action}}" method="post" enctype="multipart/form-data">
        <div class="fallback">
            <input name="file" type="file" multiple />
        </div>
        <div class="dz-message" data-dz-message>
            <div class="dz-default">
                <!--
                    According to VueJS docs / forums, "slot" gets replaced by any innerHTML
                    passed from the incoming component usage.
                -->
                <slot></slot> 
            </div>
        </div>
    </form>
</template>

最后,它在“index.html”页面中的使用方式是这样的:

<dragdropfile id="someDragDropFiles" action="/upload-please">
  Do you have files to upload?<br/>
  <b>Drop it here!</b>
</dragdropfile>

现在,虽然我可以为每个组件 HTML 模板手动输入组件名称,但我想自动执行此操作。

是否有 Vue 内部使用的特殊内置 {{binding}} 名称,以便我可以将组件名称注入页面上的结果组件?

结果如下:


...

或者我只是需要自己将其作为新的组件属性传递?如:

  • 手动调用它,如props: ["componentName", ...] 和;
  • 在模板中引用它为&lt;form class='{{componentName}}' ...&gt;

这是唯一可行的方法吗?

使用 VueJS 版本:1.0.17

(https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.17/vue.js)

【问题讨论】:

  • 请注意,解决方案不一定必须与 v1.0.17 一起使用,如果它对这种特殊情况有更好的处理,我可以升级到今天的最新版本 (v2.1.6)。

标签: javascript html css vue.js vue-component


【解决方案1】:

解决方案 #1

在使用Vue.component(...) 注册调用中的create: function() { console.log(this); } 检查对象的内容几分钟后,我在this.$options.name 属性中找到了名称。

换句话说:

...

甚至更短:

...

想一想,在每个组件模板上输入仍然需要一些手动工作,但可能有一种方法可以通过 created 方法自动附加类。


解决方案 #2

这就是我一直在寻找的自动化方法!

到这里,基本上我创建了一个包装函数,以便在我需要注册新组件时调用,它在内部调用通常的Vue.component(...) 方法。

注意:这个例子依赖于 jQuery 添加类和 underscore.js 通过_.assign 进行对象合并,但可能改为直接调用*.classList.addClass()。这些只是我熟悉的辅助方法,你喜欢用什么! :)

makeVueComponent(名称,参数)

/*
 * Creates a Vue Component with a standardized template ID name (ie: #tagname-tmp)
 * combined with given params.
 */
function makeVueComponent(name, params) {
    //Ensure params is assigned something:
    params = params || {};

    //Backup a reference of an existing *.created() method, or just an empty function
    var onCreated = params.created || function(){};

    /*
        Delete the original `created` method so the `_.assign()` call near the end
        doesn't accidentally merge and overwrite our defaultParams.created() method.
     */
    delete params.created; 

    var defaultParams = {
        //Standardized template components to expect a '-tmp' suffix
        // (this gets auto-generated by my NodeJS/Express routing)
        template: "#"+name+"-tmp",

        // This part auto-adds a class name matching the registered component-name
        created: function() {
            var $el = $(this.$options.el);
            $el.addClass(this.$options.name);

            //Then forward this to any potential custom 'created' methods we had in 'params':
            onCreated.apply(this, arguments);
        }
    };

    //Merge default params with given custom params:
    params = _.assign(defaultParams, params);

    Vue.component(name, params);
}

然后像这样使用它:

//Register your Vue Components:
makeVueComponent("dragdropfile", {props:['action']});

然后,您可以从我在解决方案 1 中提到的实际组件模板中删除那些 {{$options.name}}

【讨论】:

    猜你喜欢
    • 2022-10-15
    • 2020-03-23
    • 2019-11-21
    • 2016-07-29
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多