big*_*igp 3 html javascript css vue.js vue-component
我基本上是在尝试将 CSS 类添加到我的 VueJS 组件中 component-name
其下注册(以使所有这些特定类型的组件具有相同的样式)。
例如:
Vue.component('dragdropfile', {
// This refers to a unique template element (see HTML below)
template: "#dragdropfile-tmp",
props: ['action']
});
Run Code Online (Sandbox Code Playgroud)
在 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>
Run Code Online (Sandbox Code Playgroud)
最后,它在“index.html”页面中的使用方式是这样的:
<dragdropfile id="someDragDropFiles" action="/upload-please">
Do you have files to upload?<br/>
<b>Drop it here!</b>
</dragdropfile>
Run Code Online (Sandbox Code Playgroud)
现在,虽然我可以为每个组件 HTML 模板手动输入组件名称,但我想自动执行此操作。
{{binding}}
Vue 内部是否有任何特殊的内置名称,以便我可以将组件名称注入到页面上的结果组件中?
结果是这样的:
<form class=" dragdropfile " id="someDragDropFiles" action="/upload-please" ... >
...
</form>
还是我只需要自己将它作为新的组件属性传递?如:
props: ["componentName", ...]
和;<form class='{{componentName}}' ...>
这是唯一可行的方法吗?
( https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.17/vue.js )
big*_*igp 10
在使用注册调用create: function() { console.log(this); }
中的来检查对象持有的内容几分钟后Vue.component(...)
,我在它的名称中找到了this.$options.name
属性中。
换句话说:
<form class=" {{this.$options.name}} " ...> ... </form>
或者更短:
<form class=" {{$options.name}} " ...> ... </form>
想想看,在每个组件模板上输入仍然有点手动工作,但可能有一种方法可以通过created
方法自动附加类。
这是我一直在寻找的自动化方法!
在这里,基本上我做了一个包装函数,每当我需要注册新组件时都会调用它,它在内部调用通常的Vue.component(...)
方法。
注意:此示例依赖于jQuery添加类和underscore.js以通过 进行对象合并
_.assign
,但可能会替换为直接*.classList.addClass()
调用。这些只是我熟悉的辅助方法,喜欢的就用吧!:)
/*
* 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);
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
//Register your Vue Components:
makeVueComponent("dragdropfile", {props:['action']});
Run Code Online (Sandbox Code Playgroud)
然后,您可以{{$options.name}}
从我在解决方案 1 中提到的实际组件模板中省略那些。
归档时间: |
|
查看次数: |
9007 次 |
最近记录: |