Vue 渲染函数介绍
发布时间:2023-04-18 15:14:15 所属栏目:教程 来源:
导读:Vue 推荐在绝大多数情况下使用模板来创建你的 HTML。然而在一些场景中,你真的需要 JavaScript 的完全编程的能力。这时你可以用渲染函数,它比模板更接近编译器。
让我们用一个简单的例子来学习,这个例子里 rend
让我们用一个简单的例子来学习,这个例子里 rend
Vue 推荐在绝大多数情况下使用模板来创建你的 HTML。然而在一些场景中,你真的需要 JavaScript 的完全编程的能力。这时你可以用渲染函数,它比模板更接近编译器。 让我们用一个简单的例子来学习,这个例子里 render 函数很实用。假设我们要生成一些带锚点的标题,标题的尺寸和锚点的地址需要通过属性传递,最终生成如下格式的 DOM 结构: <h1> <a href="#hello-world"> Hello World! </a> </h1> 同学们肯定觉得这不是很简单吗,于是写下了如下示例: <!DOCTYPE html> <html lang="en"> <head> <Meta charset="UTF-8"> <Meta name="viewport" content="width=device-width, initial-scale=1.0"> <Meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="app"> <anchored-heading :level="1" href="#hello-wold">Hello World!</anchored-heading> <anchored-heading :level="2" href="#hello-wold">Hello World!</anchored-heading> <anchored-heading :level="3" href="#hello-wold">Hello World!</anchored-heading> </div> </body> <script src="https://unpkg.com/vue/dist/vue.js"></script> <script type="text/x-template" id="anchored-heading-template"> <h1 v-if="level === 1"> <a :href="href"> <slot></slot> </a> </h1> <h2 v-else-if="level === 2"> <a :href="href"> <slot></slot> </a> </h2> <h3 v-else-if="level === 3"> <a :href="href"> <slot></slot> </a> </h3> <h4 v-else-if="level === 4"> <a :href="href"> <slot></slot> </a> </h4> <h5 v-else-if="level === 5"> <a :href="href"> <slot></slot> </a> </h5> <h6 v-else-if="level === 6"> <a :href="href"> <slot></slot> </a> </h6> </script> <script type="text/javascript"> Vue.component('anchored-heading', { template: '#anchored-heading-template', props: { level: { type: Number, required: true }, href: { type: String, default: '' } } }) var vm = new Vue({ el: '#app', data() { return {} } }) </script> </html> 代码解释: JS 代码第 2-33 行,我们定义了组件的模板,通过 v-if 根据传入的 level 来控制显示的标签元素。 JS 代码第 34-46 行,我们定义了组件 anchored-heading,接收 level 和 href 两个属性参数。 HTML 代码第 2-4 行,使用了组件 anchored-heading。 上述代码虽然实现了功能,但是代码冗长,而且在每一个级别的标题中重复书写了 <a :href="href"><slot></slot></a>。那么如何才能消除这些冗余的代码呢?我们来尝试使用 render 函数重写上面的例子: <!DOCTYPE html> <html lang="en"> <head> <Meta charset="UTF-8"> <Meta name="viewport" content="width=device-width, initial-scale=1.0"> <Meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="app"> <anchored-heading :level="1" href="#hello-wold">Hello World!</anchored-heading> <anchored-heading :level="2" href="#hello-wold">Hello World!</anchored-heading> <anchored-heading :level="3" href="#hello-wold">Hello World!</anchored-heading> </div> </body> <script src="https://unpkg.com/vue/dist/vue.js"></script> <script type="text/javascript"> Vue.component('anchored-heading', { render: function (createElement) { return createElement( 'h' + this.level, // 标签名称 [createElement('a', { attrs: { href: this.href } },this.$slots.default)] ) }, props: { level: { type: Number, required: true }, href: { type: String, default: '' } } }) var vm = new Vue({ el: '#app', data() { return {} } }) </script> </html> 通过 render 函数编写的组件看起来简单多了! (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |