vue中的v-slot底层实现

高频面试题:vue中的v-slot

答案:v-shot产生的主要目的是,在组件的使用过程中可以让父组件有修改子组件内容的能力,就像在子组件里面放了个插槽,让父组件往插槽内塞入父组件中的楔子;并且,父组件在子组件中嵌入的楔子也可以访问子组件中的数据。v-slot的产生让组件的应用更加灵活。

一、具名插槽

// 在main.js文件中
let baseLayout = {template: `<div class="container"><header><slot name="header"></slot></header><main><slot></slot></main><footer><slot name="footer"></slot></footer></div>`,data() {return {url: ""};}
};

new Vue({el: "#app",template: `<base-layout><template v-slot:header><h1>title-txt</h1></template><p>paragraph-1-txt</p><p>paragraph-2-txt</p><template v-slot:footer><p>foot-txt</p></template></base-layout>`,components: {baseLayout}
}); 

当前例子渲染成真实的页面需要经历编译,虚拟DOM的获取和渲染过程。

1、编译部分

(1)ast

ast的创建过程中,当执行到闭合标签时,会进行闭合标签的属性处理closeElement(element)

function closeElement (element) {trimEndingWhitespace(element)if (!inVPre && !element.processed) {element = processElement(element, options)}// tree managementif (!stack.length && element !== root) {// allow root elements with v-if, v-else-if and v-elseif (root.if && (element.elseif || element.else)) {if (process.env.NODE_ENV !== 'production') {checkRootConstraints(element)}addIfCondition(root, {exp: element.elseif,block: element})} else if (process.env.NODE_ENV !== 'production') {warnOnce(`Component template should contain exactly one root element. ` +`If you are using v-if on multiple elements, ` +`use v-else-if to chain them instead.`,{ start: element.start })}}if (currentParent && !element.forbidden) {if (element.elseif || element.else) {processIfConditions(element, currentParent)} else {if (element.slotScope) {// scoped slot// keep it in the children list so that v-else(-if) conditions can// find it as the prev node.const name = element.slotTarget || '"default"';(currentParent.scopedSlots || (currentParent.scopedSlots = {}))[name] = element}currentParent.children.push(element)element.parent = currentParent}}// final children cleanup// filter out scoped slotselement.children = element.children.filter(c => !(c: any).slotScope)// remove trailing whitespace node againtrimEndingWhitespace(element)// check pre stateif (element.pre) {inVPre = false}if (platformIsPreTag(element.tag)) {inPre = false}// apply post-transformsfor (let i = 0; i < postTransforms.length; i++) {postTransforms[i](element, options)}} 

其中的 element = processElement(element, options)逻辑中会执行到slot相关的处理逻辑,这里主要看版本号大于2.6+的情况:

// 2.6 v-slot syntax
if (el.tag === 'template') {// v-slot on <template>const slotBinding = getAndRemoveAttrByRegex(el, slotRE)if (slotBinding) {if (process.env.NODE_ENV !== 'production') {if (el.slotTarget || el.slotScope) {warn(`Unexpected mixed usage of different slot syntaxes.`,el)}if (el.parent && !maybeComponent(el.parent)) {warn(`<template v-slot> can only appear at the root level inside ` +`the receiving component`,el)}}const { name, d
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用提到了GPT-3模型在不同样本推理形式下的表现。其,One-shot是指模型只使用一条样本进行推理,Few-shot是指模型使用少量样本进行推理(例如64条样本)。这些推理形式展示了GPT-3模型在处理自然语言任务时的能力。通过比较GPT-3模型和微调模型在TriviaQA数据集上的实验结果,可以看出,GPT-3模型在One-shot情况下已经能够与最好的微调模型相媲美,在Few-shot情况下甚至超过了微调模型的效果。这表明GPT-3模型在处理任务时具有较强的推理能力。然而,需要注意的是,GPT-3模型的参数量非常庞大(1750亿),在现有的硬件条件下,实际应用仍然存在一定的困难。此外,GPT-3模型采用的是从左到右的建模方式,对于需要结合上下文的NLU任务来说,GPT-3的效果可能不如预期。因此,虽然GPT-3模型在某些方面表现出强大的性能,但在实际应用仍然面临一些挑战。引述了GPT-3模型在处理自然语言任务时的能力,特别是在One-shot和Few-shot情况下的表现。这些实验结果表明,GPT-3模型具有较强的推理能力,并且在某些情况下能够超越微调模型的效果。然而,需要注意的是,GPT-3模型的参数量非常庞大,这对于实际应用来说可能存在一些限制。此外,GPT-3模型的建模方式也可能对某些任务的效果产生一定影响。因此,在使用GPT-3模型时需要综合考虑其性能和适用性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [四两拨千斤,训练大模型的PEFT方法](https://blog.csdn.net/Solo95/article/details/131584584)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [论文笔记:Making Pre-trained Language Models Better Few-shot Learners](https://blog.csdn.net/qq_43183860/article/details/120660668)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值