我想使用外部文件中编写的方法来更新Vue.js 3的响应式值。

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

I want to update the reactive values of Vue.js 3 using a method written in an external file

问题

Sure, here's the translated content:

我想要使用在外部文件中定义的方法来更新 Vue.js 3 的响应式值。
(例如)
我正在学习 Vue.js 3。首先,我想要将响应式值传递给在外部文件中定义的方法,并更新这些值。

我创建了一个用于测试的计数组件。在 increment1 方法中,值会得到更新(屏幕上的数字会增加),但在 increment2 方法中,值不会得到更新。需要做哪些修改来在 increment2 方法中更新值?

相关源代码

<script setup lang="ts">
import { Ref, ref } from 'vue';
import { increment as increment2 } from './increment';

const count = ref(0);
const increment1 = () => {
  count.value++;
}

</script>

<template>
  <main>
      <p>计数器数值</p>
      <p>{{ count }}</p>
      <button @click="increment1()">操作1</button>
      <button @click="increment2(count)">操作2</button>
  </main>
</template>
export const increment = (count: number) => {
  count++;
}

FW/工具版本:

Vue.js 3.2.27
TypeScript 4.7.4

英文:

I want to update the reactive values of Vue.js 3 using a method defined in an external file.
(e.g.)
I am studying Vue.js 3. First, I want to pass reactive values to a method defined in an external file and update the values.

I created a count component for testing purposes. In the increment1 method, the value gets updated (the number on the screen increases), but in the increment2 method, the value does not get updated. What modifications are necessary to update the value in the increment2 method?

Relevant Source Code

&lt;script setup lang=&quot;ts&quot;&gt;
import { Ref, ref } from &#39;vue&#39;;
import { increment as increment2 } from &#39;./increment&#39;;

const count = ref(0);
const increment1 = () =&gt; {
  count.value++;
}

&lt;/script&gt;

&lt;template&gt;
  &lt;main&gt;
      &lt;p&gt;Counter Value&lt;/p&gt;
      &lt;p&gt;{{ count }}&lt;/p&gt;
\      &lt;button @click=&quot;increment1()&quot;&gt;Action1&lt;/button&gt;
\      &lt;button @click=&quot;increment2(count)&quot;&gt;Action2&lt;/button&gt;
  &lt;/main&gt;
&lt;/template&gt;
export const increment = (count: number) =&gt; {
  count++;
}

FW/Tool versions:

Vue.js 3.2.27
TypeScript 4.7.4

答案1

得分: 1

在模板中使用时,"Refs"会自动解包,这意味着你的代码行

@click=&quot;increment2(count)&quot;

等同于

@click=&quot;increment2(count.value)&quot;

这不是响应式的。你想要做的是在你的<script>标签内发送实际的count引用,只有这样才能实现。

import { increment } from &quot;./increment.js&quot;;

const increment2 = () => {
  increment(count);
};
&lt;button @click=&quot;increment2&quot;&gt;Action2&lt;/button&gt;
英文:

Refs when used in the template are automatically unwrapped, meaning your line

@click=&quot;increment2(count)&quot;

is equivalent to

@click=&quot;increment2(count.value)&quot;

Which is not reactive. What you want to do is send the actual count ref, which can only be done within your &lt;script&gt; tag

import { increment } from &quot;./increment.js&quot;;

const increment2 = () =&gt; {
  increment(count);
};
&lt;button @click=&quot;increment2&quot;&gt;Action2&lt;/button&gt;

huangapple
  • 本文由 发表于 2023年6月12日 11:25:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453478.html
匿名

发表评论

匿名网友

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

确定