Implementing computed attribute value transfer in vue

Implement computed calculation method to pass value in vue

As we all know, the computed attribute in Vue cannot directly pass parameters, but I was bored and wanted to see if I could pass the value, so after countless attempts, I came up with the following method: We know that in
computed All calculation methods will have the step of returning data. In order to pass parameters, it should obviously be a function, and computed is different from method, so writing a function is meaningless. So the question is, why can't we return something other than a numerical value in the calculation method? It's a function. I think it's feasible. The result is really right. Here's the code:

<script setup>
import {
    
     ref } from 'vue'

defineProps({
    
    
  msg: String,
  myname:String
})

const count = ref(0)
</script>
<div>{
    
    {
    
    getcount(count)}}</div>  <!--这个count来源于script setup里面的count,我没有向外暴露-->

So the following computed should be written like this:

  computed:{
    
    
    getcount()
    {
    
    
      return (val)=>{
    
    
        console.log('当前count的值是:'+val);
      }
    }
  }

The results are as follows:
Insert image description here
It’s really ok. Although I don’t think it’s useful, it’s still good to try more interesting things. Maybe I’ll actually use them in the future hahaha

Guess you like

Origin blog.csdn.net/weixin_51295863/article/details/131736827