当前位置:网站首页>Package bedding of components
Package bedding of components
2022-07-06 13:51:00 【qq_ fifty-nine million seventy-six thousand seven hundred and s】
Preface :
As a little white, I , In every use Element-ui,Vant When waiting for the component library, I often think about a problem , What are some values passed on for , What secrets are hidden behind it , Now let's explore , The mystery of component encapsulation .
One . What is a component ?
My understanding of components , When we use some public things , At this time, it will be encapsulated into a component , Use it , That is to avoid some of the same code .
Two . How to register a component
1. Conventional registration methods
Method : Import in the parent component , Then register to use .
Usage mode : Use it as a label
2.APP.vue--------- Parent component
<template>
<div class="">
<firend />
</div>
</template>
<script>
// Import friend.vuei
import firend from "./components/firend.vue";
export default {
components: {
firend,
},
name: "",
methods: {},
};
</script>
<style scoped></style>
Child components
<template>
<div class="">
<h1> muddle along ?</h1>
</div>
</template>
<script>
export default {
name: "",
methods: {},
};
</script>
<style scoped></style>
Icon :
3. Place of use
After you package this component , It can be used in any component in the future .
3、 ... and .Vue.use Use
I have also mentioned in previous articles Vue.use Use , Now let's review .
1. What to do
answer : be used for Certified components Use our door in Element-Ui I have also seen .
2. Specific use
There's a install Method , And provide a Vue Instantiate the object .
Vue.use Inside is an object . adopt Vue.use Will trigger inside install This method .
3. Used to register a component
(1) Create a index.js Store the components to be registered
import friend from "./firend.vue";
const zujian = {
install(Vue) {
Vue.component("friend", friend);
},
};
export default zujian;
Pay attention to the component The first is string type .
(2) stay main.js Import from inside
import zujian1 from "./components/index";
Vue.use(zujian1);
(3) It can be used as a label in any component
<friend > </friend>
(4) stay install There are also things that can be done in the method , I understand the parameters that are passed on Vue It matters , It's equivalent to passing Vue The instance object of , Be similar to import Vue from "vue".
1. The name of the filter : Vue.directive('dirName',{ // Instruction name .... })
2. stay Vue Prototype chain mounting method : Vue.prototype.$atters=function(){ }
3. filter :Vue.filter(' The name of the command ',' Callback function ').
Four . Now let's introduce slots
1. understand , What is your understanding of slots , My understanding is that I want to reserve a place , I don't know what to put in this position . Equivalent to occupying space .
2. Several ways of slots .
Default slot
A named slot
Scope slot
3. The default slot is also called anonymous slot
APP.Vue
<template>
<div class="">
<friend> The value to be passed to the sub component </friend>
</div>
</template>
<script>
import friend from "./components/firend.vue";
export default {
components: {
friend,
},
name: "",
methods: {},
};
</script>
<style scoped></style>
friend.vue
<template>
<div class="">
<slot></slot>
</div>
</template>
<script>
export default {
name: "",
methods: {},
};
</script>
<style scoped></style>
Icon :
4. A named slot
Think of names , That is, a slot with a name . A name corresponds to a position
APP.vue
<template>
<div class="">
<friend #header> To the head </friend>
<friend #main> Pass it to the middle </friend>
<friend #footer> To the tail </friend>
</div>
</template>
<script>
import friend from "./components/firend.vue";
export default {
components: {
friend,
},
name: "",
methods: {},
};
</script>
<style scoped></style>
friend.vue
<template>
<div class="">
<slot name="header"></slot>
<slot name="main"></slot>
<slot name="footer"></slot>
</div>
</template>
<script>
export default {
name: "",
methods: {},
};
</script>
<style scoped></style>
Icon :
5. Scope slot ---------- Used to transfer data
It is similar to the process of a son passing on his father .
App.vue
<template>
<div class="">
<friend v-slot="aa">{
{ aa }} </friend>
</div>
</template>
<script>
import friend from "./components/firend.vue";
export default {
components: {
friend,
},
name: "",
methods: {},
};
</script>
<style scoped></style>
friend.vue
<template>
<div class="">
<slot :data="name"></slot>
</div>
</template>
<script>
export default {
data() {
return {
name: {
age: 20,
},
};
},
name: "",
methods: {},
};
</script>
<style scoped></style>
Icon :
5、 ... and .V-model Value transfer on components
1. Definition resolution , This is also the way components transfer values , Pass value from father to son , The child can also modify the value in the parent component .
2.APP.vue
<template>
<div class="">
<friend v-model="name"> </friend>
</div>
</template>
<script>
import friend from "./components/firend.vue";
export default {
data() {
return {
name: " Zhang San ",
};
},
components: {
friend,
},
name: "",
methods: {},
};
</script>
<style scoped></style>
firend.vue
<template>
<div class="">
{
{ value }}
<button @click="fn"> Click to modify the value in the parent component </button>
</div>
</template>
<script>
export default {
// Receive the value from the parent component
props: {
value: {
type: String,
default: " Wang Wu ",
},
},
name: "",
methods: {
fn() {
this.$emit("input", (this.value = " Li Liu "));
},
},
};
</script>
<style scoped></style>
3. By default, the custom value passed in the past is value, Custom events are input
<friend v-model="name"> </friend>、
Equivalent to
<friend :value="name" @input="input"> </friend>
4. How to change the custom value ?
It's needed at this time model This attribute .
Format :
model: {
prop: " Value to change ",
event: " Events to be changed ",
},
App.Vue
<template>
<div class="">
<friend v-model="name"> </friend>
<!-- <friend :value="name" @input="input"> </friend> -->
</div>
</template>
<script>
import friend from "./components/firend.vue";
export default {
data() {
return {
name: " Zhang San ",
};
},
components: {
friend,
},
name: "",
methods: {},
};
</script>
<style scoped></style>
friend.Vue
<template>
<div class="">
{
{ changvalue }}
<button @click="fn"> Click to modify the value in the parent component </button>
</div>
</template>
<script>
export default {
// Receive the value from the parent component \
model: {
prop: "changvalue",
event: "input123",
},
props: {
changvalue: {
type: String,
},
},
name: "",
methods: {
fn() {
this.$emit("input123", (this.value = " Li Liu "));
},
},
};
</script>
<style scoped></style>
6、 ... and . Sync Use
1. understand
Sync It is also a way to pass values ,V-model Such value transfer can only be carried out once , and Sync You can use it multiple times .
V-model The default is value and input, and Sync The default is value and update, Fixed writing .
2. principle
// Normal father to son : <com1 :a="num" :b="num2"></com1> // add sync Then the father passed on to the son : <com1 :a.sync="num" .b.sync="num2"></com1> // It is equivalent to <com1 :a="num" @update:a="val=>num=val" :b="num2" @update:b="val=>num2=val"></com1>
Equivalent to a callback .
3. Code :
<template>
<div class="">
<friend :abc.sync="a" :nba.sync="b"> </friend>
<!-- <friend :value="name" @input="input"> </friend> -->
</div>
</template>
<script>
import friend from "./components/firend.vue";
export default {
data() {
return {
a: "123",
b: " Wang Zong ",
};
},
components: {
friend,
},
name: "",
methods: {},
};
</script>
<style scoped></style>
friend.vue
<template>
<div class="">
{
{ abc }}
{
{ nba }}
<button @click="fn"> hold 123 Change to 456</button>
<button @click="fn1"> Change president Wang into holding roses </button>
</div>
</template>
<script>
export default {
props: {
abc: {
type: Number,
default: 321,
},
nba: {
type: String,
},
},
name: "",
methods: {
fn() {
this.$emit("update:abc", 456);
},
fn1() {
this.$emit("update:nba", " President Peng ");
},
},
};
</script>
<style scoped></style>
Icon :
边栏推荐
- 5. Function recursion exercise
- Meituan dynamic thread pool practice ideas, open source
- [au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
- 稻 城 亚 丁
- 强化学习系列(一):基本原理和概念
- 8. C language - bit operator and displacement operator
- 2. C language matrix multiplication
- 7-4 散列表查找(PTA程序设计)
- . Net6: develop modern 3D industrial software based on WPF (2)
- MySQL事务及实现原理全面总结,再也不用担心面试
猜你喜欢
MySQL事务及实现原理全面总结,再也不用担心面试
1143_ SiCp learning notes_ Tree recursion
Mortal immortal cultivation pointer-1
Matlab opens M file garbled solution
Programme de jeu de cartes - confrontation homme - machine
5. Function recursion exercise
仿牛客技术博客项目常见问题及解答(三)
6. Function recursion
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
Mortal immortal cultivation pointer-2
随机推荐
7-7 7003 组合锁(PTA程序设计)
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
About the parental delegation mechanism and the process of class loading
【VMware异常问题】问题分析&解决办法
Programme de jeu de cartes - confrontation homme - machine
Custom RPC project - frequently asked questions and explanations (Registration Center)
受检异常和非受检异常的区别和理解
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
fianl、finally、finalize三者的区别
Implementation of count (*) in MySQL
[the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
Miscellaneous talk on May 14
ABA问题遇到过吗,详细说以下,如何避免ABA问题
【数据库 三大范式】一看就懂
关于双亲委派机制和类加载的过程
SRC挖掘思路及方法
4. Branch statements and loop statements
Safe driving skills on ice and snow roads
hashCode()与equals()之间的关系
实验七 常用类的使用(修正帖)