简体   繁体   English

vue.js - 动态 html 标签(使用变量)

[英]vue.js - dynamic html tag (using variable)

I was wondering if there was any way to dynamically set the tags of an html element.我想知道是否有任何方法可以动态设置 html 元素的标签。 Eg例如

var element = "ol";

<{element}> some content </{element}>

Easier method is to just use component element, like this:更简单的方法是只使用component元素,如下所示:

<component :is="elType">...</component>

Just set elType in data to whatever type of element you want it to be (ie div , h1 etc)只需将数据中的elType设置为您想要的任何类型的元素(即divh1等)

Demo: https://codesandbox.io/s/strange-kapitsa-zbtok?fontsize=14&hidenavigation=1&module=%2Fsrc%2FApp.vue&theme=dark演示: https ://codesandbox.io/s/strange-kapitsa-zbtok?fontsize=14&hidenavigation=1&module=%2Fsrc%2FApp.vue&theme=dark

You may want to look into Render Function , jsx is also supported in Vue.js2.您可能想查看Render Functionjsx也支持 jsx。
Here's a simple example.这是一个简单的例子。

 var element = 'ol' Vue.component('custom', { render: function (createElement) { return createElement( element, this.$slots.default ) }, }) new Vue({ el: "#app" })
 <script src="https://unpkg.com/vue/dist/vue.js"></script> <div id="app"> <custom>abc</custom> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM