渲染函数

x-template

  • 引入外面组件文件写法

    <template>
    <h1 v-if="level === 1">
    <slot></slot>
    </h1>
    <h2 v-else-if="level === 2">
    <slot></slot>
    </h2>
    <h3 v-else-if="level === 3">
    <slot></slot>
    </h3>
    <h4 v-else>
    <slot></slot>
    </h4>
    </template>
    <script>
    exports default{
    props: {
    level: {type: Number}
    }
    }
    </script>
    <anchored-heading :level="2">自定义标题</anchored-heading>
    
    <script>
    import anchoredHeading from '@/components/anchoredHeading'
    components: { anchoredHeading },
    </script>
  • html 页面内组件写法, 属性 id 方便取到组件内容

    <script type="text/x-template" id="anchored-heading-template">
    <h1 v-if="level === 1">
    <slot></slot>
    </h1>
    <h2 v-else-if="level === 2">
    <slot></slot>
    </h2>
    <h3 v-else-if="level === 3">
    <slot></slot>
    </h3>
    <h4 v-else>
    <slot></slot>
    </h4>
    </script>
    <anchored-heading :level="2">自定义标题</anchored-heading>
    
    <script>
    components: {
    'anchoredHeading': {
    template: '#anchored-heading-template',
    props: {
    level: {
    type: Number,
    required: true
    }
    }
    },
    },
    </script>
  • 渲染函数渲染类似组件

    <anchored-heading :level="2">自定义标题</anchored-heading>
    
    <script>
    components: {
    'anchoredHeading': {
    render: function (createElement) {
    return createElement(
    'h' + this.level, // 标签名称
    this.$slots.default // 子节点数组
    )
    },
    props: {
    level: {
    type: Number,
    required: true
    }
    }
    },
    },
    </script>

createElement()

createElement(
// {String | Object | Function}
// 一个 HTML 标签名、组件选项对象,或者 resolve 了上述任何一种的一个 async 函数。必填项。
'div',
// {Object}
// 一个与模板中 attribute 对应的数据对象。可选。
{
// 与 `v-bind:class` 的 API 相同,
// 接受一个字符串、对象或字符串和对象组成的数组
class': {
foo: true,
bar: false
},
// 与 `v-bind:style` 的 API 相同,
// 接受一个字符串、对象,或对象组成的数组
style: {
color: 'red',
fontSize: '14px'
},
// 普通的 HTML attribute
attrs: { id: 'foo' },
// 组件 prop
props: { myProp: 'bar' },
// DOM property
domProps: { innerHTML: 'baz' },
// 事件监听器在 `on` 内,
// 但不再支持如 `v-on:keyup.enter` 这样的修饰器。
// 需要在处理函数中手动检查 keyCode。
on: { click: this.clickHandler },
// 仅用于组件,用于监听原生事件,而不是组件内部使用
// `vm.$emit` 触发的事件。
nativeOn: { click: this.nativeClickHandler },
// 自定义指令。注意,你无法对 `binding` 中的 `oldValue`
// 赋值,因为 Vue 已经自动为你进行了同步。
directives: [{
name: 'my-custom-directive',
value: '2',
expression: '1 + 1',
arg: 'foo',
modifiers: { bar: true }
}],
// 作用域插槽的格式为
// { name: props => VNode | Array<VNode> }
scopedSlots: {
default: props => createElement('span', props.text)
},
// 如果组件是其它组件的子组件,需为插槽指定名称
slot: 'name-of-slot',
// 其它特殊顶层 property
key: 'myKey',
ref: 'myRef',
// 如果你在渲染函数中给多个元素都应用了相同的 ref 名,
// 那么 `$refs.myRef` 会变成一个数组。
refInFor: true
},
// {String | Array}
// 子级虚拟节点 (VNodes),由 `createElement()` 构建而成, 也可以使用字符串来生成“文本虚拟节点”。可选。
// this.$slots.default 可以直接加入使用组件时插入插槽的组件
[
'先写一些文字',
createElement('h1', '一则头条'),
createElement(MyComponent, {
props: { someProp: 'foobar' }
})
]
)

VUE学习-渲染函数的更多相关文章

  1. vue render 渲染函数

    vue render 渲染函数 经常看到使用render渲染函数的示例,而且在一些特殊情况下,确实更好使用,可以更加有效地细分组件,因而借助vue-element-admin来学习一波 render函 ...

  2. Vue.js 渲染函数, JSX(未掌握,未学完)

    渲染函数 , JSX(没完成学习) 基础: 实例属性:vm.$slots default 属性包括了所有没有被包含在具名插槽中的节点. 渲染函数: render: function(createEle ...

  3. vue 学习 渲染、v-指令

    vue渲染 在组件中data是一个方法里面的值要是一个对象return出去 export default { name: "HelloWorld", data() { return ...

  4. Vue基础-渲染函数-插槽

    Vue 测试版本:Vue.js v2.5.13 先看个插槽的例子: <div id="app"> <child > <span slot-scope= ...

  5. Vue基础-渲染函数-父子组件-传递数据

    Vue 测试版本:Vue.js v2.5.13 做了个 demo,把父子组件的数据都绑定到 Vue 实例 app 上,注释中的 template 相对好理解些 <div id="app ...

  6. Vue学习笔记:编译过程

    碰到是否有template选项时,会询问是否要对template进行编译: 在template编译(渲染成UI)有一个过程.模板通过编译生成AST,再由AST生成Vue的渲染函数,渲染函数结合数据生成 ...

  7. Render渲染函数和JSX

    1.Render函数:render是用来替换temlate的,需要更灵活的模板的写法的时候,用render. 官网API地址:https://cn.vuejs.org/v2/guide/render- ...

  8. Vue:模板&渲染函数学习

    模板&渲染函数区别: 1.代码量:模板代码重复逐行拼写,渲染函数可以迭代拼接方式实现重复代码. 2.函数式组件中应用:基于模板的函数式组件需要手动添加特性和事件,给予渲染函数的函数是组件使用c ...

  9. 【js】vue 2.5.1 源码学习 (十一) 模板编译compileToFunctions渲染函数

    大体思路(九) 本节内容: 1. compileToFunctions定位 1. compileToFunctions定位 ==> createCompiler = createCompiler ...

  10. 【js】 vue 2.5.1 源码学习(六) initProxy initLifeCycle 渲染函数的作用域代理

    大体思路 (五) 1. initProxy 渲染函数的作用域代理 ==> es6 如果支持proxy (hasProxy) 就用proxy 不支持就用 defineProperty() prox ...

随机推荐

  1. 旧酒换新瓶,新版M1/M2芯片Macos(Ventura)安装古早版本Python2.7(Python2.x)

    向下兼容特性是软件开发系统的一个重要指标,它是指一个新的系统或者软件能够与旧的系统或软件兼容并正常运行.这意味着旧系统或软件可以在新系统或软件中使用,而不会出现问题.向下兼容对于提高软件或系统的可用性 ...

  2. CVE-2022-32532 Apache Shiro 身份认证绕过

    漏洞名称 CVE-2022-32532 Apache Shiro 身份认证绕过 利用条件 Apache Shiro < 1.9.1 漏洞原理 使用RegexRequestMatcher进行权限配 ...

  3. vulnhub靶场之HACKATHONCTF: 2

    准备: 攻击机:虚拟机kali.本机win10. 靶机:HackathonCTF: 2,下载地址:https://download.vulnhub.com/hackathonctf/Hackathon ...

  4. Ubuntu 安装 office

    推荐安装 LibreOffice wget https://free.nchc.org.tw/tdf/libreoffice/stable/7.4.3/deb/x86_64/LibreOffice_7 ...

  5. 【高并发】AQS中的CountDownLatch、Semaphore与CyclicBarrier用法总结

    CountDownLatch 概述 同步辅助类,通过它可以阻塞当前线程.也就是说,能够实现一个线程或者多个线程一直等待,直到其他线程执行的操作完成.使用一个给定的计数器进行初始化,该计数器的操作是原子 ...

  6. 用GPU来运行Python代码

    简介 前几天捣鼓了一下Ubuntu,正是想用一下我旧电脑上的N卡,可以用GPU来跑代码,体验一下多核的快乐. 还好我这破电脑也是支持Cuda的: $ sudo lshw -C display *-di ...

  7. Salesforce LWC学习(四十一) If:true 即将弃用?

    本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_dir ...

  8. 《Terraform 101 从入门到实践》 Terraform在公有云GCP上的应用

    <Terraform 101 从入门到实践>这本小册在南瓜慢说官方网站和GitHub两个地方同步更新,书中的示例代码也是放在GitHub上,方便大家参考查看. Terraform支持的公有 ...

  9. Eureka、Consul、Zookeeper注册中心总结

    组件名 编写语言 CAP 服务健康检查 对外暴露接口 Springcloud集成 Eureka Java AP 可配支持(安全机制) Http √ Consul Go CP 支持 Http/DNS √ ...

  10. react中redux怎么使用

    一.redux是什么? redux 就是react 全局状态管理,作用是存放全局数据 二.核心 state:存放数据 reducer:修改仓库数据 是一个函数,参数一:仓库中的数据,参数2:行为 ac ...