在Vue组件中查找变量名称

huangapple go评论61阅读模式
英文:

Finding variable names in vue components

问题

我有一个程序中的变量名列表
我想查看我的Vue组件中所有使用了哪些组件中的变量。

例如:

let x = ['test_one', 'test_two'];

查找所有组件中是否使用了某些内容

// Example.vue
<template>
     {{ x }}
</template>

<script setup>
    const test_one = ref('hello');
    const x = ref('abcd');
</script>

查找 test_one 是否存在于 Example.vue
查找 test_two 是否存在于 Example.vue

您知道我可以查找组件中的变量的方法吗?

英文:

I have a list of variable names in my program
I want to look in all my components in vue and see which component each variable is used in.

For example :

let x = [&#39;test_one&#39;, &#39;test_two&#39;];

find in all components for uses someone

// Example.vue
&lt;template&gt;
     {{ x }}
&lt;/template&gt;

&lt;script setup&gt;
    const test_one = ref(&#39;hello&#39;);
    const x = ref(&#39;abcd&#39;);
&lt;/script&gt;

find test_one exists in Example.vue
find test_two exists in Example.vue

Do you know of a way I can look for variables in my components?

答案1

得分: 1

如果我理解正确,您可以在子组件中使用 defineExpose 宏,然后在父组件中对该组件使用一个 ref:

Example.vue

&lt;template&gt;
     {{ x }}
&lt;/template&gt;

&lt;script setup&gt;
   import {ref} from &#39;vue&#39;
    
    const test_one = ref(&#39;hello&#39;);
    const x = ref(&#39;abcd&#39;);

   defineExpose({test_one,x})

&lt;/script&gt;

在父组件中:

&lt;script setup&gt;
import { ref } from &#39;vue&#39;
import Example from &#39;./Example.vue&#39;
const example = ref()

console.log(&quot;Example value: &quot;, example.value)
&lt;/script&gt;

&lt;template&gt;
  
  &lt;Example ref=&quot;example&quot;/&gt;
&lt;/template&gt;
英文:

If I understood correctly, you can use defineExpose macro in child component, then use a ref on that component in parent one :

Example.vue

&lt;template&gt;
     {{ x }}
&lt;/template&gt;

&lt;script setup&gt;
   import {ref} from &#39;vue&#39;
    
    const test_one = ref(&#39;hello&#39;);
    const x = ref(&#39;abcd&#39;);

   defineExpose({test_one,x})

&lt;/script&gt;

in parent component :

&lt;script setup&gt;
import { ref } from &#39;vue&#39;
import Example from &#39;./Example.vue&#39;
const example =ref()

console.log(&quot;Example value: &quot;,example.value)
&lt;/script&gt;

&lt;template&gt;
  
  &lt;Example ref=&quot;example&quot;/&gt;
&lt;/template&gt;

huangapple
  • 本文由 发表于 2023年7月31日 19:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定