当前位置:网站首页>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 :
边栏推荐
- Service ability of Hongmeng harmonyos learning notes to realize cross end communication
- 强化学习系列(一):基本原理和概念
- 【手撕代码】单例模式及生产者/消费者模式
- FAQs and answers to the imitation Niuke technology blog project (II)
- A piece of music composed by buzzer (Chengdu)
- . How to upload XMIND files to Jinshan document sharing online editing?
- 3. C language uses algebraic cofactor to calculate determinant
- 渗透测试学习与实战阶段分析
- The latest tank battle 2022 full development notes-1
- 为什么要使用Redis
猜你喜欢
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
Reinforcement learning series (I): basic principles and concepts
一段用蜂鸣器编的音乐(成都)
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
Read only error handling
仿牛客技术博客项目常见问题及解答(一)
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
A piece of music composed by buzzer (Chengdu)
2022 Teddy cup data mining challenge question C idea and post game summary
6. Function recursion
随机推荐
The difference between overloading and rewriting
[面试时]——我如何讲清楚TCP实现可靠传输的机制
简单理解ES6的Promise
6. Function recursion
简述xhr -xhr的基本使用
附加简化版示例数据库到SqlServer数据库实例中
The difference between abstract classes and interfaces
Why use redis
Difference and understanding between detected and non detected anomalies
[graduation season · advanced technology Er] goodbye, my student days
Principles, advantages and disadvantages of two persistence mechanisms RDB and AOF of redis
FAQs and answers to the imitation Niuke technology blog project (I)
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
Nuxtjs快速上手(Nuxt2)
Zatan 0516
MATLAB打开.m文件乱码解决办法
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
【九阳神功】2020复旦大学应用统计真题+解析
SRC mining ideas and methods
1143_ SiCp learning notes_ Tree recursion