在 vue 渲染中使用插槽的示例代码
最编程
2024-07-14 12:02:00
...
本文介绍了vue Render中slots的使用的实例代码,有需要了解vue Render中slots用法的朋友可参考。希望此文章对各位有所帮助。
render 中 slot 的一般默认使用方式如下:
this.$slots.default 对用 template的<slot>的使用没有name 。
想使用多个slot 的话。需要对slot命名唯一。使用this.$slots.name 在render中添加多个slot。
<body> <div class="" id="app"> <myslot> <div>this is slot</div> </myslot> </div> <script> Vue.component('myslot',{ render:function(createElement){ var he=createElement('div',{domProps:{innerHTML:'this child div'}}); return createElement('div',[he,this.$slots.default]) } }); var app=new Vue({ el:'#app' }) </script> </body>
多个slot的使用
<body> <div class="" id="app"> <myslot> <div slot="name1">this is slot</div> <div slot="name2">The position is slot2 </div> </myslot> </div> <script> Vue.component('myslot',{ render:function(createElement){ var he=createElement('div',{domProps:{innerHTML:'this child div'}}); return createElement('div',[he,this.$slots.name2,this.$slots.name1]) } }); var app=new Vue({ el:'#app' }) </script> </body>
在vue2.1.0新添加了scope(虽然感觉有点怪,但是用习惯了,还蛮好用的)。同样给出一般使用和多个使用示例,
<body> <div class="" id="app"> <myslot> <template scope="props"> <div>{{props.text}}</div> </template> </myslot> </div> <script> Vue.component('myslot',{ render:function(createElement){ var he=createElement('div',{domProps:{innerHTML:'this child div'}}); return createElement('div',[he,this.$scopedSlots.default({ text:'hello scope' })]) } }); var app=new Vue({ el:'#app' }) </script> </body>
多个$scopedSlot的使用
<body> <div class="" id="app"> <myslot> <template slot="name2" scope="props"> <div>{{props.text}}</div> </template> <template slot="name1" scope="props"> <span>{{props.text}}</span> </template> </myslot> </div> <script> Vue.component('myslot',{ render:function(createElement){ var he=createElement('div',{domProps:{innerHTML:'this child div'}}); return createElement('div', [he, this.$scopedSlots.name1({ text:'hello scope' }), this.$scopedSlots.name2({ text:'$scopedSlots using' })]) } }); var app=new Vue({ el:'#app' }) </script> </body>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Vue 插槽的使用
推荐阅读
-
OpenCV 高级图形用户界面(9)中更改指定窗口位置的函数 moveWindow 的使用--代码示例
-
通过 Vue 中的 v-bind 增强样式控制--(通过 v-bind 操作类和操作样式属性的详细说明,附示例和代码)
-
OpenCV 高级图形用户界面(6)中获取指定窗口图像矩形区域的函数 getWindowImageRect 的使用--代码示例
-
Matlab 编程示例 24:freexyn 在 b-site 中读取手写 mnist 数据集的 Matlab 代码 -4. 读取和转换 mnist 数据集的 Matlab 程序代码
-
Java 8新特性探究(十三)JavaFX 8新特性以及开发2048游戏-JavaFX历史## 跟java在服务器端和web端成绩相比,桌面一直是java的软肋,于是Sun公司在2008年推出JavaFX,弥补桌面软件的缺陷,请看下图JavaFX一路走过来的改进 从上图看出,一开始推出时候,开发者需使用一种名为JavaFX Script的静态的、声明式的编程语言来开发JavaFX应用程序。因为JavaFX Script将会被编译为Java bytecode,程序员可以使用Java代码代替。 JavaFX 2.0之后的版本摒弃了JavaFX Script语言,而作为一个Java API来使用。因此使用JavaFX平台实现的应用程序将直接通过标准Java代码来实现。 JavaFX 2.0 包含非常丰富的 UI 控件、图形和多媒体特性用于简化可视化应用的开发,WebView可直接在应用中嵌入网页;另外 2.0 版本允许使用 FXML 进行 UI 定义,这是一个脚本化基于 XML 的标识语言。 从JDK 7u6开始,JavaFx就与JDK捆绑在一起了,JavaFX团队称,下一个版本将是8.0,目前所有的工作都已经围绕8.0库进行。这是因为JavaFX将捆绑在Java 8中,因此该团队决定跳过几个版本号,迎头赶上Java 8。 ##JavaFx8的新特性 ## ###全新现代主题:Modena 新的Modena主题来替换原来的Caspian主题。不过在Application的start方法中,可以通过setUserAgentStylesheet(STYLESHEET_CASPIAN)来继续使用Caspian主题。 参考http://fxexperience.com/2013/03/modena-theme-update/ ###JavaFX 3D 在JavaFX8中提供了3D图像处理API,包括Shape3D (Box, Cylinder, MeshView, Sphere子类),SubScene, Material, PickResult, LightBase (AmbientLight 和PointLight子类),SceneAntialiasing等。Camera类也得到了更新。从JavaDoc中可以找到更多信息。 ###富文本 强化了富文本的支持 ###TreeTableView ###日期控件DatePicker 增加日期控件 ###用于 CSS 结构的公共 API
-
使用matplotlib在Python中实现正弦信号的时域波形和频谱图示例
-
在Java JSP中实现打印功能的技巧与代码示例
-
使用Lambda表达式在LINQ中的OrderBy和GroupBy操作及实战示例
-
在Vue中添加HTML代码的技巧
-
Vue 3中keep-alive的使用方法及activated钩子详解和scrollBehavior在vue-router中的应用