So fragen Sie Werte in Json-Objekten mithilfe von Schlüsseln in Vuejs 3 ab
P粉413307845
P粉413307845 2023-09-02 20:14:08
0
2
498
<p>Ich bin neu bei Vuejs und muss über die Referenz „id“ auf das „Label“ zugreifen. </p> <p><code>const Wizard_steps = ref([ { id: 1, label: „Schritt 1“ }, { id: 2, label: „Schritt 2“ }, { id: 3, label: „Schritt 3 " } ]) const currentstep = ref([Nummer]) </code></p> <p>Wenn currentstep === 2, möchte ich auf die Daten von „step 2“ zugreifen, ich habe versucht: <code>wizard_steps.filter(id=2).label</code></p>
P粉413307845
P粉413307845

Antworte allen(2)
P粉337385922

列表要点在这里。

您可以使用Array::find()在数组中查找值。 另外,您可以使用computed来获得一个响应式的值,以便在模板中使用。此外,我认为您可以使用reactive来代替ref来处理步骤。如果步骤不会改变,您可以删除reactive,因为在这种情况下它是不需要的。

<script setup>
    
  import { reactive, ref, computed } from 'vue';

  const wizardSteps = reactive([ { id: 1, label: "步骤 1" }, { id: 2, label: "步骤 2" }, { id: 3, label: "步骤 3" } ]);
  const currentStep = ref(1)

  const currentLabel = computed(() => wizardSteps.find(({id}) => id === currentStep.value).label);

</script>

<template>
  <div>当前步骤 {{ currentStep }}</div>
  <div>当前标签 {{ currentLabel }}</div>
  <button @click="currentStep = currentStep === wizardSteps.length ? 1 : currentStep + 1">下一步</button>
</template>
P粉668804228

Solution

在Vue中,我们使用响应式变量来影响DOM的变量。在这里,我们可以声明wizard_steps,稍后可以从const变量中创建的对象的.value键中获取它们 - 您可以在提供的代码中看到这一点。我们需要创建一个变量,可以操作所选ID。根据所选ID,我们可以使用一个简单的JavaScript数组和find()函数来找到所选步骤,并显示其标签。使用computed()函数可以根据current_step_id的响应式变化调整与当前步骤关联的标签,该函数还声明了一个响应式变量。但是,不能直接操作此变量。相反,当其内部使用的响应式变量发生变化时,更新其.value

您可以查看此示例代码。

const { createApp, ref, reactive, computed } = Vue

const app = createApp({
  setup() {
    // steps
    const wizard_steps = reactive([
      { id: 1, label: "step 1" },
      { id: 2, label: "step 2" },
      { id: 3, label: "step 3" }
    ])
    
    // current selected id
    const current_step_id = ref(1)
    
    // current label by selected id
    const current_step_label = computed(() => {
      // find current step by current id
      const current_step = wizard_steps.find((step) => step.id === current_step_id.value)
      
      // error, if step not found
      if (current_step === undefined) return `step not found by ID(${current_step_id.value})`
      
      // return current label by current step
      return current_step.label
    })
    
    // change current_step_id by button
    const select_step = (step) => {
      current_step_id.value = step.id
    }
    
    return { wizard_steps, current_step_id, current_step_label, select_step }
  }
}).mount('#app')
<script src="https://unpkg.com/vue@3.3.4/dist/vue.global.prod.js"></script>

<div id="app">
  <h2>Manipulate ID</h2>
  <h4>By Input</h4>
  <!-- input for can change id -->
  <input v-model="current_step_id" type="number" />
  
  <h4>or By Buttons</h4>
  <!-- button for select id -->
  <button v-for="step of wizard_steps" @click="select_step(step)">
    Select step #{{ step.id }} ({{ step.label }})
  </button>
  
  <h2>Check Current Label</h2>
  <!-- check label for selected id -->
  <div>{{ current_step_label }}</div>
</div>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage