Vue中render方法的使用
2018-01-30 11:01
239 查看
先说一下对官网上demo的个人理解:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
虽然使用template定义组件的方法非常的直观,但是这样会造成代码过长。可以使用render的方法
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
下面是一个slot具名分发的demo:介绍了creatElement的用法:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
所创建的组件的demo结果如下:
<!DOCTYPE html> <html> <head> <title>Vue的render方法说明</title> <script src="vue.js"></script> </head> <body> <div id="app"> <child :level="1"> hello world </child> </div> <script type="text/x-template" id="anchored-heading-template"> <div> <h1 v-if="level === 1"> <slot></slot> </h1> <h2 v-if="level === 2"> <slot></slot> </h2> <h3 v-if="level === 3"> <slot></slot> </h3> <h4 v-if="level === 4"> <slot></slot> </h4> <h5 v-if="level === 5"> <slot></slot> </h5> <h6 v-if="level === 6"> <slot></slot> </h6> </div> </script> <script type="text/javascript"> Vue.component('child', { template: '#anchored-heading-template', props: { level: { type: Number, required: true } } }); new Vue({ el: "#app" }) </script> </body> </html>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
虽然使用template定义组件的方法非常的直观,但是这样会造成代码过长。可以使用render的方法
<!DOCTYPE html> <html> <head> <title>Vue的render方法说明</title> <script src="vue.js"></script> </head> <body> <div id="app"> <child :level="1"> hello world </child> </div> <script type="text/javascript"> Vue.component('child', { render:function (createElement) { var body=this.$slots.default; //this.$slots返回了一个组件分发下来的元素和内容 //this.$slots.default返回了具名的内容 return createElement( 'h'+this.level, //this.level是利用v-bind注入到组件中的level body ) }, //因为vue中组件父组件无法向子组件注入内容。所以我们需要通过 //v-bind定义一个key,value向子组件注入内容。所要接收的值也需要在定义组件时的props属性中的定义一下 props:{ level:{ } } }); new Vue({ el: "#app" }) </script> </body> </html>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
下面是一个slot具名分发的demo:介绍了creatElement的用法:
<!DOCTYPE html> <html> <head> <title>Vue的render方法说明</title> <script src="vue.js"></script> </head> <body> <div id="app"> <child> <p slot="header">this is header</p> <p slot="center">this is center</p> <p slot="footer">this is footer</p> </child> </div> <script type="text/javascript"> Vue.component('child', { render: function (createElement) { var header=this.$slots.header; var center=this.$slots.center; var footer=this.$slots.footer; //createElement第一个参数是标签名,第二个参数是值 return createElement('div',[ createElement('div', header), createElement('div', center), createElement('div', footer), ]) } }); new Vue({ el: "#app" }) </script> </body> </html>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
所创建的组件的demo结果如下:
相关文章推荐
- Vue.js render方法使用详解
- Vue中render方法的使用
- Vue中render方法的使用详解
- Vue.js render方法使用详解
- vuejs模板使用方法
- Django框架中render_to_response()函数的使用方法
- Django框架中render_to_response()函数的使用方法
- 关于Vue.js 使用v-cloak后仍显示变量的解决方法
- ASP.NET MVC3 局部页面@RENDERBODY @RENDERPAGE@RENDERSECTION使用方法详细说明
- IPicture中Render()函数的使用方法
- vue插件使用方法
- 使用vue时Module build failed: TypeError: this._init is not a function错误的解决方法
- vue2.0 keep-active使用方法
- vue-router:嵌套路由的使用方法
- Vue中使用set方法过程的一个小发现
- 【工作日志】--关于Vue.js中<slot>的使用方法及注意事项
- asp.net mvc3 局部页面@RenderBody @RenderPage@RenderSection使用方法详细说明
- asp.net mvc3 局部页面@RenderBody @RenderPage@RenderSection使用方法详细说明
- asp.net控件开发基础(2) ----------明白使用Render方法呈现自定义控件
- H5+Mui文件配置 vue-resource基本使用方法