从导入的函数修改组件的变量

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

Modify a Component's variable from an imported function

问题

下面是你要翻译的代码部分:

export default {
    data() {
        return {
            var1: val1
        }
    }
}
import { reset_state, get_random_entity, get_start_entity } from '../../functions/entity_component_functions.ts';
<button id="entity-btn-getstarted" @click="get_random_entity(entity_isLoaded, entity_content_data)" class="entity-btn">
    Do something!
</button>
<button id="entity-btn-getstarted2" @click="get_start_entity(entity_isLoaded, entity_content_data, selected_entity_type)" class="entity-btn">
    Get an entity of your selected type!
</button>
function manage_entitycomponent_data() {
    const entity_isLoaded = ref(false);
    const entity_content_data = ref({ "test": "test" });

    function load_entity(json) {
        const random_index = get_random_number(json.length);
        entity_content_data.value = json[random_index];
        entity_isLoaded.value = true;
    }

    return { entity_isLoaded, entity_content_data }
}

请注意,我只翻译了代码部分,没有包括问题的其余内容。如果你需要更多帮助,请随时提问。

英文:

I have a Vue component in which I have defined variables in

export default {
        data() {
           return {
             var1: val1
           }
        }
}

Depending on the value of these variables things are displayed in my component. The content is used for stuff like v-if and also for displaying the actual content.

I use functions to manipulate the contents of those variables (e.g. to pull from an API). For better readability, I would like to keep the functions in a separate file. So I import them like this:

import {reset_state, get_random_entity, get_start_entity } from &#39;../../functions/entity_component_functions.ts&#39;;

How can I share my variables defined in the Component to the functions in this file entity_component_functions.ts so they can modify their content and the component reacts upon changes?

I am happy to read in the documentation if you tell me where to look or what is the technical term for what I search for.

UPDATE:

I was pointed to Composables which do look great, but I am not sure how to use them in my case.

In my component, I have a couple of buttons that do different things when clicked. But they - in the end - have to update what is displayed in the component (thus the same variable).

However, in the Composables example with the mouse, the Composable itself is the only one that can update its managed variables and it does so by defining what to do in case of an event.

But I have different functions (see below) that are called depending on which button is clicked. How can they update the Composable's variables (entity_isLoaded, entity_content_data)? They are also defined in functions.ts - but they cannot call load_entity(), right?

component:

&lt;button id=&quot;entity-btn-getstarted&quot; @click=&quot;get_random_entity(entity_isLoaded, entity_content_data)&quot; class=&quot;entity-btn&quot;&gt;
    Do something!
&lt;/button&gt;
&lt;button id=&quot;entity-btn-getstarted2&quot; @click=&quot;get_start_entity(entity_isLoaded, entity_content_data, selected_entity_type)&quot; class=&quot;entity-btn&quot;&gt;
    Get an entity of your selected type!
&lt;/button&gt;

functions.ts:

function manage_entitycomponent_data() {
    const entity_isLoaded = ref(false);
    const entity_content_data = ref({&quot;test&quot;: &quot;test&quot;}); 

    function load_entity(json) {
        const random_index = get_random_number(json.length);
        entity_content_data.value = json[random_index];
        entity_isLoaded.value = true;
    }

    return {entity_isLoaded, entity_content_data}
}

答案1

得分: 0

如果你正在使用Vue3,我认为你正在寻找的是Composables。这可能需要基本的组合式API知识,但你可以在Options API中使用它们。

更新:

实际上,你可以导入和更改可重用组合式中的变量。

首先,你的functions.ts必须导出你的函数

import { ref } from "vue";

export function manage_entitycomponent_data() {
  const entity_isLoaded = ref(false);
  const entity_content_data = ref({"test": "test"});

  function load_entity(json) {
      const random_index = get_random_number(json.length);
      entity_content_data.value = json[random_index];
      entity_isLoaded.value = true;
  }

  return { entity_isLoaded, entity_content_data, load_entity }
}

只需确保在你的.vue组件中返回所需的每个变量和函数。

组合式的一个良好实践是使用use{WhatYourHandling}来调用你的函数,在这种情况下,我们可以像这样调用它:useEntity

import { ref } from "vue";

export function useEntity() {
  // ...
}

之后,你需要将你的可重用组合式导入到你的.vue组件中。你正在使用Options API,因此返回的绑定必须在setup()内调用,以便在this和模板中暴露出来(根据官方链接中的文档)。

<script>
// or <script lang="ts"> if you're using typescript in your project

import { useEntity } from "THE_PATH"

export default {
  setup() {
    const { entity_isLoaded, entity_content_data, load_entity } = useEntity()

    return { entity_isLoaded, entity_content_data, load_entity }
  },
  methods: {
    // ...
  },
  data() {
    return {
      // ...
    }
  },
  // the rest...
}
</script>

设置完成后,你可以像在此示例中一样自由使用你的变量和函数,既可以在template中,也可以在script中:

<template>
  <div>
    {{ entity_isLoaded }} - {{ entity_content_data }}
    <button @click="isLoaded(true)">Yes it's loaded </button>
    <button @click="isLoaded(false)">No it's not loaded </button>
  </div>
</template>

<script>
import { useEntity } from "@/composables/functions"

export default {
  setup() {
    const { entity_isLoaded, entity_content_data, load_entity } = useEntity()

    return { entity_isLoaded, entity_content_data, load_entity }
  },
  methods: {
    isLoaded(value) {
      this.entity_isLoaded = value
    }
  },
  mounted() {
    this.load_entity({})
  }
}
</script>

最后,你只需要在你的可重用组合式中使用export,并使用setup()在你的Vue组件中调用每个变量和函数。

希望我理解了你的问题!

英文:

If you're using Vue3 I think what you're looking for is Composables. This may require a basic knowledge of Composition API but you can use them with Options API.

UPDATE:

You actually can import and change variables from your composable.

First of all your functions.ts must export your function

import { ref } from &quot;vue&quot;;

export function manage_entitycomponent_data() {
  const entity_isLoaded = ref(false);
  const entity_content_data = ref({&quot;test&quot;: &quot;test&quot;}); 

  function load_entity(json) {
      const random_index = get_random_number(json.length);
      entity_content_data.value = json[random_index];
      entity_isLoaded.value = true;
  }

  return { entity_isLoaded, entity_content_data, load_entity }
}

Just make sure to return every variable and function you need in your .vue component.

A good practice for composables is to call your function with use{WhatYourHandling} in this case we can call it like useEntity

import { ref } from &quot;vue&quot;;

export function useEntity() {
  // ...
}

After that you need to import your composable into your .vue component. You're using Options API, so the returned bindings must be called inside setup() to be exposed to this and the template. (From the offical Docs in the link)

&lt;script&gt;
// or &lt;script lang=&quot;ts&quot;&gt; if you&#39;re using typescript in your project

import { useEntity } from &quot;THE_PATH&quot;

export default {
  setup() {
    const { entity_isLoaded, entity_content_data, load_entity } = useEntity()

    return { entity_isLoaded, entity_content_data, load_entity }
  },
  methods: {
    // ...
  },
  data() {
    return {
      // ...
    }
  },
  // the rest...
}
&lt;/script&gt;

After this setup you can use your variables and functions as you like both in template and script like in this example:

&lt;template&gt;
  &lt;div&gt;
    {{ entity_isLoaded }} - {{ entity_content_data }}
    &lt;button @click=&quot;isLoaded(true)&quot;&gt;Yes it&#39;s loaded &lt;/button&gt;
    &lt;button @click=&quot;isLoaded(false)&quot;&gt;No it&#39;s not loaded &lt;/button&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import { useEntity } from &quot;@/composables/functions&quot;

export default {
  setup() {
    const { entity_isLoaded, entity_content_data, load_entity } = useEntity()

    return { entity_isLoaded, entity_content_data, load_entity }
  },
  methods: {
    isLoaded(value) {
      this.entity_isLoaded = value
    }
  },
  mounted() {
    this.load_entity({})
  }
}
&lt;/script&gt;

In the end you just need to use export in your Composable and call every variable and function using setup() in your Vue component.

Hope I got your question right!

huangapple
  • 本文由 发表于 2023年3月31日 21:56:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899366.html
匿名

发表评论

匿名网友

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

确定