【问题标题】:Responsive select menu does not display array values响应式选择菜单不显示数组值
【发布时间】:2021-09-19 17:52:15
【问题描述】:

我有一个根据所选标签显示值的导航栏

现在我想在移动视图中做同样的事情,为此,我有一个选择菜单而不是导航栏

但价值没有改变

但是当我选择不同的值时,值并没有改变,我做错了什么?问候

CodePen

代码:

<template>
  <div>
     <div class="sm:hidden">
      <label for="tabs" class="sr-only">Select a tab</label>
      <select id="tabs" name="tabs" class="block w-full focus:ring-indigo-500 focus:border-indigo-500 border-gray-300 rounded-md">
        <option v-for="tab in tabs" :key="tab.name" :selected="tab.current">{{ tab.name }}</option>
      </select>
        <div >
    <div v-for="tab in tabs" @click="changeTab(tab)"  :key="tab.name" :href="tab.href" class="px-12" :class="[tab.current || 'hidden']">
      {{ tab.id }} - {{ tab.name }} - {{  tab.href }} - {{ tab.title }} - {{tab.imageSrc}}
    </div>
  </div>
    </div>
 
    <div class="hidden sm:block">
     <nav class="flex space-x-4 " aria-label="Tabs" >
        <a v-for="tab in tabs" @click="changeTab(tab)"  :key="tab.name" :href="tab.href" :class="[tab.current ? 'bg-purple-700 text-white' : 'text-purple-700 hover:text-gray-700', 'px-12 py-2 font-medium text-sm rounded-full font-bold text-lg']" >
          {{ tab.name }}
        </a>
      </nav>
    </div>
     <div class="hidden sm:block">
    <div v-for="tab in tabs" @click="changeTab(tab)"  :key="tab.name" :href="tab.href" class="px-12" :class="[tab.current || 'hidden']">
      {{ tab.id }} - {{ tab.name }} - {{  tab.href }} - {{ tab.title }} - {{tab.imageSrc}}
    </div>
  </div>
  </div>
</template>

<script lang="ts">
import { ref } from 'vue'
export default {
  setup() {
    const tabs = ref([
      { id: 1 , title: 'test title one', imageSrc:'/programs/test1.png' , content: '', name: 'LOREM', href: '#test1', current: true },
      { id: 2 , title: 'test title two',  imageSrc:'/programs/test2.png', content: '', name: 'IPSUM', href: '#test2', current: false },
      { id: 3 , title: 'test title three', imageSrc:'/programs/test3.png', content: '', name: 'PDF', href: '#test3', current: false },
      { id: 4 , title: 'test title three', imageSrc:'/programs/test3.png', content: '', name: 'PDF', href: '#test3', current: false },
      { id: 5 , title: 'test title three', imageSrc:'/programs/test3.png', content: '', name: 'PDF', href: '#test3', current: false },
      { id: 6 , title: 'test title three', imageSrc:'/programs/test3.png', content: '', name: 'PDF', href: '#test3', current: false },
      
    ])
    
    const  changeTab = (selectedTab) => {
      tabs.value.map(t => {
        t.id === selectedTab.id ? t.current = true : t.current = false
      });
    }
    
    return { tabs, changeTab }
    
  },
    computed: {
    img() {
      return `./images/modal/${encodeURIComponent(this.tabs[0].imageSrc)}.png`
    },
  },
}
</script>


<style lang="scss" scoped>
  nav {
    display: flex;
    gap: 20px; 
    background: #E5E5E5; 
    border-radius: 20px;
    width: 100%;
justify-content: space-between;
}
</style>

【问题讨论】:

    标签: javascript vue.js vuejs3


    【解决方案1】:

    您的&lt;select&gt; 标签没有绑定。通常你应该有一个v-bind 或至少一个change 处理程序来更新数据。

    您的change 处理程序可能如下所示:

    <select
      @change="changeTab($event.target.value)"
    >
    ...
    </select>
    

    【讨论】:

    • 我试过了,现在内容不显示了
    【解决方案2】:

    尝试从option而不是从下面的div调用方法:

    <div class="sm:hidden">
      <label for="tabs" class="sr-only">Select a tab</label>
      <select id="tabs" name="tabs" class="block w-full focus:ring-indigo-500 focus:border-indigo-500 border-gray-300 rounded-md" >
        <option v-for="tab in tabs" @click="changeTab(tab)" :key="tab.name" :selected="tab.current">{{ tab.name }}</option>
      </select>
      <div v-for="tab in tabs" :key="tab.name" :href="tab.href" class="px-12" :class="[tab.current || 'hidden']">
      {{ tab.id }} - {{ tab.name }} - {{  tab.href }} - {{ tab.title }} - {{tab.imageSrc}}
      </div>
    <div >
    

    【讨论】:

    • 如果我这样做,我只是添加选择选项的内容,但我想添加波纹管选项
    • 现在检查我更新了答案
    • 我尝试但没有更改选择更改时的内容,我使用更改更新 CodePen
    • 我正在查看您的 codepen 并且一切正常?只需从答案中复制/粘贴代码,而不是 codepen 中的代码。
    猜你喜欢
    • 2019-12-20
    • 2013-11-15
    • 1970-01-01
    • 2017-09-20
    • 2012-03-12
    • 1970-01-01
    • 2014-04-07
    • 2017-07-16
    • 2020-11-12
    相关资源
    最近更新 更多