当前位置:网站首页>21.mixin混入详解
21.mixin混入详解
2022-07-23 18:53:00 【风落不归处】
1.钩子函数
混入对象的钩子将在组件自身钩子之前调用
App页面
<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>结果图:

由此我们可以知道混入对象的钩子将在自身钩子之前调用,且mixin中能够打印出App中a3的值
2.普通方法合并
当混合值为对象的选项时,例如methods、components、directive,将被混合为一个对象,两个对象键名冲突时,取组件对象的键值对
APP页面
<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.局部混入
组件
<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.全局混入
在main.js加入以下代码
Vue.mixin({
data() {
return {
msg: "hello"
}
},
methods: {
test1() {
console.log("mixin test1")
}
}
})在组件中直接引用
<template>
<div>{
{ msg }}</div>
</template>
<script>
export default {
data() {
return {
}
},
mounted() {
this.test1()
}
}
</script>5.总结
(1)功能:可以把多个组件共用的配置提取成一个混入对象
(2)使用方式:
第一步定义混合:
{
data() {...},
methods: {...}
}...
第二步使用混入:
全局混入:Vue.mixin(xxx)
局部混入:mixins:['xxx']
边栏推荐
- Cannot read properties of null (reading ‘pickAlgorithm‘)
- Energy principle and variational method note 17: generalized variational principle (identification factor method)
- Codeworks round 805-808 [partial solution]
- 梅科尔工作室-小熊派开发笔记2
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and use the theme of ggpubr package_ The classic2 function sets the visual image to classic theme with axis lines
- Edge cloud | 1. overview
- 【Unity项目实践】关卡解锁
- What if redis breaks down?
- How important is 5g dual card and dual access?
- Cannot read properties of null (reading ‘pickAlgorithm‘)
猜你喜欢

Leetcode 228. 汇总区间(可以,已解决)

3D point cloud course (VI) -- 3D target detection

Cannot read properties of null (reading ‘pickAlgorithm‘)

Energy principle and variational method note 14: summary + problem solving

Redux求和案例详解版教程

New product listing | A-share floor derivatives market point
![[untitled]](/img/2f/cfac9dcac7466cf3ac5601b0fadf03.png)
[untitled]

Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4

web安全入门-ssh测试与防御

Compiler llvm MLIR introductions llvm backend instruction
随机推荐
Hongke dry goods | teaches you how to parse floating-point data in MODBUS
Adobe Acrobat two powerful plug-ins
Powercli add esxi host to vCenter
Typescript新数据类型Symbol的使用
[unity project practice] entrustment
I deliberately leave a loophole in the code. Is it illegal?
How should testing cope with the new development mode?
PostgreSQL sequence cache 参数导致的数值序列不连续 有间隔 gap
osgearth使用sundog公司的triton海洋和silverlining云彩效果
能量原理与变分法笔记17:广义变分原理(识别因子方法)
Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC
Leetcode - the nearest sum of three numbers
能量原理与变分法笔记14:总结+问题的求解
Eight common SQL misuses in MySQL
Paddle implementation, multidimensional time series data enhancement, mixup (using beta distribution to make continuous random numbers)
Principe de l'énergie et méthode variationnelle note 19: principe de l'énergie résiduelle minimale + principe du travail possible
Set asp Net MVC site default page is the specified page
[untitled]
How important is 5g dual card and dual access?
Leetcode 216. 组合总和 III