vue3中mixin的使用方法

 

<template>
    <div class="box">
        {{mData.name}}
    </div>
</template>
 
<script setup>
import { ref} from 'vue'
// 导入
import userMix from "@/common/mixins/user";
// 拿到数据
const { mData } = userMix();
console.log(mData)
 
</script>
<style lang="scss" scoped>
</style>

user.js代码

import {
  ref,
  watch,
  onMounted
} from 'vue'
export default function (userData) {
  const mData = ref(null)
  onMounted(() => {
    getData()
  })
  watch(() => data, (newVal, oldVal) => {
    console.log('watch', newVal, oldVal)
  }, {
    deep: true
  })
  const getData = () => {
    if (!userData) {
        mData.value = {
            name: 'lily',
            age: 10
        }
    } else {
        mData.value = userData.value
    }
    
  }
 
  // 暴露出去
  return {
    mData,
    getData
  }
}

 

https://blog.csdn.net/bingjilingvsh2o/article/details/135925874

 

posted @ 2024-07-20 10:41  小小强学习网  阅读(285)  评论(0编辑  收藏  举报