当前位置:网站首页>p67 mixin 混入/混合:两个组件共享一个配置
p67 mixin 混入/混合:两个组件共享一个配置
2022-08-09 05:09:00 【LIZHUOLONG1】
p67 mixin 混入/混合:两个组件共享一个配置
- 一定是先引入、再配置进去、使用的
school
<template>
<div>
<!-- showname在混合当中 -->
<h2 @click="showName">学校名称:{
{
name }}</h2>
<h2>学校地址:{
{
address }}</h2>
</div>
</template>
<script>
//引入一个hunhe
// 局部混合:在此引入混合
// import { hunhe, hunhe2 } from '../mixin'
export default {
name: 'School',
data() {
return {
name: '尚硅谷',
address: '北京',
x: 666 //以data中的x为准,混合中再有这个变量就不起作用
}
},
mounted() {
console.log('nihaoa')
},
// 局部混合:在此配置混合。要是很多文件有需要引入混合,那100个文件都要写一遍吗?那肯定不是->引出全局混合
// mixins: [hunhe, hunhe2],
}
</script>
student
<template>
<div>
<h2 @click="showName">学生姓名:{
{
name }}</h2>
<h2>学生性别:{
{
sex }}</h2>
</div>
</template>
<script>
// import { hunhe, hunhe2 } from '../mixin'
export default {
name: 'Student',
data() {
return {
name: '张三',
sex: '男'
}
},
// hunhe有的、hunhe2有的 内容会组合到一块给这个组件用(此处为局部混合)
// mixins: [hunhe, hunhe2]
}
</script>
App.vue
<template>
<div>
<School/>
<hr>
<Student/>
</div>
</template>
<script>
import School from './components/School'
import Student from './components/Student'
export default {
name:'App',
components:{
School,Student}
}
</script>
main.js
import Vue from 'vue'
import App from './App.vue'
import {
hunhe, hunhe2} from './mixin' //引用
Vue.config.productionTip = false
Vue.mixin(hunhe) //配置
Vue.mixin(hunhe2)
// 放在全局上,会输出四次'你好啊',vm上面有,3个vc上面有
new Vue({
el:'#app',
render: h => h(App)
})
mixin.js(名称可以改变)
// 要把配置交出去,用export,不然别人没法引入
// 分别暴露:引入的时候就得 import {hunhe} from ...
export const hunhe = {
methods: {
showName(){
alert(this.name)
}
},
mounted() {
console.log('你好啊!')
},
}
export const hunhe2 = {
data() {
return {
x:100,
y:200
}
},
}
边栏推荐
猜你喜欢
matlab simulink球杆控制系统的模糊PID控制设计
ELTEK电源维修SMPS5000SIL整流器模块故障分析及特点
如何剪裁svg并压缩
【Harmony OS】【ARK UI】Lightweight Data Storage
【HMS core】【ML kit】机器学习服务常见问题FAQ
22-08-08 西安 尚医通(04)MongoDB命令、MongoTemplate、MongoRepository
mysql content does not exist error
TP6的安装与测试
【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
制作频谱应用FFT心得
随机推荐
Address book (dynamic version) (C language) (VS)
The development trend of software testing
Masked AutoEncoder论文及实现
STM32系列单片机使用心得
制作频谱应用FFT心得
【LeetCode】136. 只出现一次的数字
Parameters in dynamic libraries cannot be modified through macro definitions or global variables in header files
unity urp 实现遮挡显示角色轮廓
神经网络预测应力应变-单轴实验
【Harmony OS】【ARK UI】轻量级数据存储
【Harmony OS】【ARK UI】Lightweight Data Storage
C进阶-C语言文件操作
【计算机网络-哈工大】---学习笔记(下)---(一)网络安全、密码学基础、对称、公钥、身份认证、数字签名、KDC\CA
阻塞队列实现异步消费
【计算机网络-哈工大】---学习笔记(下)---(二)Web安全威胁、SSL\IPsec、虚拟专用网、防火墙
HAL库的使用之Cube配置编码器输入捕获模式
保存Simulink仿真模型为图片或者PDF的方法
DS3231模块使用
TASSEL software imports plink format file error
php将在线远程文件写入临时文件