Vue component - input

Lu Zhen 2020-11-08 20:15:25
vue component input


Before the start

  • I need you. Vue Have some understanding , It's better to have read the official tutorial .
  • contains Vue3 The content of , If you already know Vue2, But I'm not familiar with Vue3, You don't have to be afraid .
  • It involves specific API Less details of use .
  • I will Vue The component is divided into three parts : Input 、 Internal processing 、 Output ( In fact, it can be understood as a function ), This article is about “ Input ” The content of .

Catalog

Text

How to understand “ Input ”

Input , That is to say, to pass values to components or external factors that can cause changes in components . This article classifies all input methods of components , In order to clearly understand the data sources of components and external sources of changes .

Classification of component inputs

I divide the input into two categories , One is : Explicit input , The second is : Implicit input . I will introduce these two categories respectively .

 Classification of component inputs

Explicit input

Explicit input , seeing the name of a thing one thinks of its function , Explicit data passed to components .

props

As shown below , utilize props Explicitly to HelloWorld Component passes in a string “Welcome to Your Vue.js App”. You can almost rely on props towards HelloWorld Component passes in any value , but HelloWorld The internal part of the component needs to be in the props Define the name of the received property in the .

 <HelloWorld msg="Welcome to Your Vue.js App" />

  • props The transmission value is Parent towards Sub level One of the ways to pass values .
  • towards props Pass in a callback function , Can also be realized Sub level towards Parent Pass value . here , Not only is “ Input ” It can also be used as a kind of “ Output ”( Pass the value to through the parameters of the callback function Parent ).
  • props Medium default It can be understood as the default value of function parameters .type It can be understood as the type of function parameter .validator The verification of function parameters is understandable .required It can be understood as whether the function parameter is an optional parameter .
summary

 Classification of component inputs -> Explicit input ->props

emits

" Input " Not only can it be a specific number , It can also be an event (emits) Callback function for . This event contains HTML The original event of , It also includes component custom events .

summary

 Classification of component inputs -> Explicit input ->emits

$attrs

When a component is passed a props or emits As defined in attribute When ,Vue The value will be automatically added to the root node by default attribute in . As shown below ,Date The node composition of a component :

 <template>
<div>
// Other contents
</div>
</template>

We call :

 <Date class="my-date" />

Actually render to DOM Medium Date Components for :

 <div class="my-date">
//Date Component rendering of other DOM
</div>

As shown above , Incoming class Added to the <div> On .

but , When a component has more than two root nodes (Vue2 Only one root node is allowed , and Vue3 More than one is allowed ),Vue What will be done with ?
Vue It's up to you to choose , Use v-bind="$attrs" Assign values to any of them A root node .

If you don't want to divide props and emits Any value beyond the definition ? To configure :inheritAttrs: false.

If the root node doesn't want , And want to give the child node ? Use v-bind="$attrs" Assign values to any of them A dime node .

summary

$attrs Belongs to instance property , Generally used to receive native HTML Attribute of element . Is it used , It's entirely up to the component itself . >

  • $attrs yes Parent towards Sub level One of the ways to pass values .

 Classification of component inputs -> Explicit input ->$attrs

slot

stay Vue You can use the slot (slot) To do content distribution , Slots can also be considered as a kind of component “ Input ”. As shown below ,Date Component added to HelloWorld Component's default In slot .

 <HelloWorld>
<Date msg="Hi" />
</HelloWorld>

  • Direction Date Component in msg The value of , yes Grandparents towards Descendants One of the ways to pass values .
  • when Scope slot When it comes to Parent towards Sub level One of the ways to pass values ( take HelloWorld Value inside component , Pass to Date Components ).
summary

 Classification of component inputs -> Explicit input ->slots

directive

This mainly refers to user-defined instructions . So why is a custom instruction considered a kind of “ Input ” 了 ? Because the custom instruction can be applied to DOM Element to do the underlying operation , Affect the state of the component . For more details about the components, see the corresponding official documentation .

summary

 Classification of component inputs -> Explicit input ->directives

Explicit input summary

Let me take a look at the usage of a component that contains all explicit input :

  • msg by props
  • class by $attrs
  • click by emits
  • v-hour For customization directive
  • Date and User Components for slot, among User Components pass values through scope slots
 <HelloWorld msg="Welcome to Your Vue.js App" class="my-hello" @clik="clickFn">
<Date v-hour="time" />
<template v-slot:user="slotProps">
<User :name="slotProps.name" />
</template>
</HelloWorld>

Further observation , We can find out : All explicit input is basically implemented by means of instructions !($attrs With the exception of )

  • props With the help of v-bind
  • emits With the help of v-on
  • slot With the help of v-slot( The default slot may sometimes not be used explicitly )
  • Custom instructions, let alone , It's an instruction in itself
About v-model

v-model Instructions are also common input sources in components , But its essence is props and emits The grammar sugar of , So it doesn't count in “ Input ” Inside the .

Implicit input

Implicit input , Refers to all the situations that can affect the state of a component except for explicit input . He's not “ Explicit ” Pass in , But it has an impact on the inside of the component .

inject

Inside the component, through inject You can get the data passed in by grandparents , Because you can't see it at a glance “ Input ”, And it's not always possible to know which component passed in the value , So inject (inject), I classify it as implicit input .

summary

 Classification of component inputs -> Implicit input ->inject

mixin

Whether it's the global mix (mixin) Or just mix in the components , It is a kind of implicit transmission and processing method which is not easy to observe , So mix in , Also referred to as implicit input .

summary

 Classification of component inputs -> Implicit input ->mixin

extends

Extend another component (extends), And mixin similar . So expand , It's also a kind of implicit input .

summary

 Classification of component inputs -> Implicit input ->extends

globalProperties

adopt app.config.globalProperties Add global properties (property), Any component instance can access ; And directly in Vue Add something like . Because it's a global attribute , It's a kind of “ Implicit ” Pass value , So it's a kind of implicit input .

  • yes Grandparents towards Descendants One of the ways to pass values .
summary

 Classification of component inputs -> Implicit input ->globalProperties

Implicit input summary

Implicit input is usually used to transfer values between grandchildren or to increase the reusability of components .

Summary of input

Finally, let's summarize ,“ Input ” What are the total :

  • Explicit input :
    • props
    • emits
    • $attrs
    • slot
    • directive
  • Implicit input :
    • inject
    • mixin
    • extend
    • globalProperties

Last

  • Reprint please indicate the source
  • Impropriety , Welcome to communicate and correct
版权声明
本文为[Lu Zhen]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple