如何在外部JS文件中更改Vue变量的状态?

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

How to change the state of a Vue variable in an external JS file?

问题

让我们假设我有一个Vue组件

```javascript
import { foo } from '路径';
export default {
  data() {
    return {
      inputValue: "",
    };
  },

我如何创建一个导入的JavaScript函数,该函数接受一个inputValue,修改它,然后将inputValue的更新状态返回给Vue组件?

我尝试将inputValue作为参数传递给函数foo(),在函数内部更改其值,然后返回它,但我得到了一个undefined值。

我正在创建一个外部函数,因为修改**inputValue**只是函数的一小部分,我不希望我的代码变得混乱。


<details>
<summary>英文:</summary>

Let&#39;s say I have a vue component:

import {foo} from 'path';
export default {
data() {
return {
inputValue: "",
};
},


How can I create imported JavaScript function that takes an `inputValue`, modifies it, and then returns the updated state of the `inputValue` to the vue component?

I tried passing the `inputValue` as a parameter to the function `foo()`, changing its value inside the function, and returning it, but I got an `undefined` value.

I&#39;m creating an external function because modifying **`inputValue`** is just a small part of the function, and I don&#39;t want my code to become cluttered.

</details>


# 答案1
**得分**: 0

我认为你应该在小型应用中使用Vue [Composables][1],如果你的状态周围有很多逻辑,可以使用[Pinia][2]。

查看Vue文档:

- [Composables][1]
- [状态管理][3]

这是一个非常简单的示例:

```javascript
const { createApp, ref } = Vue;

const useStore = (value) => {
    const input = ref(value);
    const add = () => input.value++;
    return { input, add };
}

const App = {
    setup() {
        const { input, add } = useStore(1);
        return { input, add };
    }
}

const app = createApp(App);
app.mount('#app');
#app {
    line-height: 2;
}

[v-cloak] {
    display: none;
}
<div id="app">
    <input type="number" v-model="input" /> <button @click="add()">+1</button>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
英文:

I think you should use Vue Composables for small applications or Pinia if you have a lot of logic around your state.

Check the Vue Docs on

Here is a very simple sample

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

const { createApp, ref  } = Vue;

const useStore = (value) =&gt; {
    const input = ref(value);
    const add = () =&gt; input.value++;
    return { input, add }
}

const App = {
  setup() {
    const { input, add } = useStore(1);
    return { input, add }  
  }
}

const app = createApp(App)
app.mount(&#39;#app&#39;)

<!-- language: lang-css -->

#app {
  line-height: 2;
}

[v-cloak] {
  display: none;
}

<!-- language: lang-html -->

&lt;div id=&quot;app&quot;&gt;
  &lt;input type=&quot;number&quot; v-model=&quot;input&quot; /&gt; &lt;button @click=&quot;add()&quot;&gt;+1&lt;/button&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 0

你尝试过在方法上调用它吗?

methods: {
    updateInputValue() {
      this.inputValue = foo(this.inputValue);
    },
  },

更新输入的示例:

  <div>
    <input v-model="inputValue" />
    <button @click="updateInputValue">更新输入值</button>
  </div>
英文:

Have you tried calling it on a method?

methods: {
    updateInputValue() {
      this.inputValue = foo(this.inputValue);
    },
  },

Example on updating the input:

  &lt;div&gt;
    &lt;input v-model=&quot;inputValue&quot; /&gt;
    &lt;button @click=&quot;updateInputValue&quot;&gt;Update Input Value&lt;/button&gt;
  &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年2月24日 05:57:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550739.html
匿名

发表评论

匿名网友

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

确定