当前位置:网站首页>21. Mix in details
21. Mix in details
2022-07-23 20:07:00 【Where the wind falls】
1. Hook function
The hook of the object will be called before the component itself hooks
App page
<template>
<div class="app">
</div>
</template>
<script>
import mixin from "@/mixins/mixin";
export default {
name: "App",
mixins: [mixin],
data() {
return {
a1: "1",
a3: "3"
}
},
created() {
console.log("App created ",this.a1)
console.log("App created ",this.a2)
}
}
</script>mixin.js
<template>
<div class="app">
</div>
</template>
<script>
import mixin from "@/mixins/mixin";
export default {
name: "App",
mixins: [mixin],
data() {
return {
a1: "1",
a3: "3"
}
},
created() {
console.log("App created ",this.a1)
console.log("App created ",this.a2)
}
}
</script>Result chart :

From this, we can know that the hook of the mixed object will call before its own hook , And mixin Can print App in a3 Value
2. Common method consolidation
When the blend value is the option of the object , for example methods、components、directive, Will be mixed into an object , When two object key names conflict , Take the key value pair of the component object
APP page
<template>
<div class="app">
</div>
</template>
<script>
import mixin from "@/mixins/mixin";
export default {
name: "App",
mixins: [mixin],
data() {
return {
a1: "1",
a3: "3"
}
},
methods: {
test: function() {
console.log("App test")
}
},
mounted() {
this.test()
this.test2()
}
}
</script>mixin.js
const mixin = {
data() {
return {
}
},
methods: {
test: function() {
console.log("mixin test")
},
test2: function() {
console.log("mixin test2")
}
},
}
export default mixin;
3. To mix in partially
Components
<template>
<div>{
{msg}}</div>
</template>
<script>
import mixin from '../mixins/mixin'
export default {
mixins: [mixin],
data() {
return {
}
},
mounted() {
this.mixinMethod()
}
}mixin
const mixin = {
data() {
return {
msg: "hello"
}
},
methods: {
test1() {
console.log("mixin ", this.msg)
}
}
}
export default mixin;4. The whole thing is in
stay main.js Add the following code
Vue.mixin({
data() {
return {
msg: "hello"
}
},
methods: {
test1() {
console.log("mixin test1")
}
}
})Reference directly in the component
<template>
<div>{
{ msg }}</div>
</template>
<script>
export default {
data() {
return {
}
},
mounted() {
this.test1()
}
}
</script>5. summary
(1) function : The configuration shared by multiple components can be extracted into a mixed object
(2) Usage mode :
The first step is to define the blend :
{
data() {...},
methods: {...}
}...
Step 2 use blending :
The whole thing is in :Vue.mixin(xxx)
To mix in partially :mixins:['xxx']
边栏推荐
- 梅科尔工作室-华为14天鸿蒙设备开发实战笔记四
- 【AR学习】-- 二、 环境搭建
- 2022山东老博会,山东养老展,中国国际养老服务业展9月举办
- 2022DASCTF MAY
- PostgreSQL sequence cache 参数导致的数值序列不连续 有间隔 gap
- Paddle implementation, multidimensional time series data enhancement, mixup (using beta distribution to make continuous random numbers)
- 3D point cloud course (VII) -- feature point description
- Configure MySQL master-slave replication with mysqldump or mydumper
- Energy principle and variational method note 16: solution of virtual displacement principle
- 重装系统后故障(报错:reboot and select proper boot deviceor insert boot media in selected boot device)
猜你喜欢

梅科尔工作室-华为14天鸿蒙设备开发实战笔记六

Mekol Studio - Little Bear Development Notes 2
![Codeworks round 805-808 [partial solution]](/img/02/6fb3a963eac165cbae64cdfc0735e1.png)
Codeworks round 805-808 [partial solution]

Energy principle and variational method note 15: solution of differential element method

Leetcode 152. product maximum subarray (brute force cracking can actually pass!)

【Unity项目实践】关卡解锁

MySQL master-slave replication

Introduction to web security SSH testing and defense

osgearth使用sundog公司的triton海洋和silverlining云彩效果

MySQL 数据恢复 —— 使用 data 目录
随机推荐
osgearth2.8编译siverlining云效果
李鸿章 临终诗 临终奏折 全文
能量原理与变分法笔记18:虚力原理
New product listing | A-share floor derivatives market point
Mecol Studio - Little Bear Development Notes 3
2、 MFC windows and messages
Leetcode 152. 乘积最大子数组(暴力破解居然可以通过!)
Leetcode 238. 除自身以外数组的乘积
Powercli moves virtual machines from host01 host to host02 host
能量原理與變分法筆記19:最小餘能原理+可能功原理
重装系统后故障(报错:reboot and select proper boot deviceor insert boot media in selected boot device)
I deliberately leave a loophole in the code. Is it illegal?
[interview: concurrent Article 22 multithreading: reentrantlock]
能量原理与变分法笔记17:广义变分原理(识别因子方法)
Atelier macoll - notes de développement de la secte de l'ours 2
Codeworks round 805-808 [partial solution]
MySQL master-slave replication
[untitled]
ODrive应用 #6 编码器
安装svn 汉化包 也不能设置中文[通俗易懂]