首页  >  问答  >  正文

Vuejs 3中使用键在Json对象中查询值的方法

<p>我对Vuejs还不太熟悉,需要通过'id'引用来访问'label'。</p> <p><code>const Wizard_steps = ref([ { id: 1, 标签: "第 1 步" }, { id: 2, 标签: "第 2 步" }, { id: 3, 标签: "第 3 步" } ]) const currentstep = ref([编号]) </code></p> <p>如果currentstep === 2,我想访问"step 2"的数据,我尝试过: <code>wizard_steps.filter(id=2).label</code></p>
P粉413307845P粉413307845412 天前467

全部回复(2)我来回复

  • P粉337385922

    P粉3373859222023-09-03 15:59:55

    列表要点在这里。

    您可以使用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>

    回复
    0
  • P粉668804228

    P粉6688042282023-09-03 15:58:31

    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>

    回复
    0
  • 取消回复