【问题标题】:Only select post Im clicking on (v-for @click vue.js) but its selecting all saved posts because of the v-for, what can I use instead?只选择我点击的帖子(v-for @click vue.js),但由于 v-for 而它选择了所有已保存的帖子,我可以用什么代替?
【发布时间】:2022-11-17 22:54:28
【问题描述】:

我正在做一个小博客项目来练习 vue.js。我正在使用组合 API 脚本设置。当你写一篇文章时,它会保存在 localStorage 中。当您单击博客中的文本时,它会打开一个叠加层和一个大尺寸的博客文本。只是它不仅显示了我点击的那个,还显示了其他较早的博客文章。我怎样才能集中注意力,所以它只显示我点击的那个。我使用 tailwind 作为 css。

在这个链接上你可以测试你自己,但是在我制作它之前我有一些虚拟文本所以 note.text 将被显示; https://unique-sprinkles-d1a6cd.netlify.app/

<template>
    <div v-if="showModal" @click="showModal = false" class="absolute w-screen h-screen bg-black opacity-75 z-10 flex cursor-pointer items-center justify-center">
        <div v-for="note in notes" :key="note.id" class="bg-white  hover:border-red-400 p-4 rounded-full text-2xl font-bold"><p>{{ note.text }}</p></div>
    </div>
<div class="w-6/12 text-center m-auto">
    <h1 class="text-4xl font-bold text-indigo-500 m-5">Blog Posts</h1>
    <div  class="border-2 border-slate-100 rounded-3xl hover:border-red-400" v-for="note in notes" :key="note.id" > 
        <div class="relative flex flex-col m-0 justify-around cursor-pointer">
            <button class="absolute top-0 right-0 w-5 h-5 cursor-pointer rounded-full hover:bg-red-400 p-2.5" @click="deleteBtn(note)">x</button>
            <img class="w-24 h-24 p-3.5 rounded-full" :src="note.img"> 
            <p class="text-xs text-right pr-3.5"> {{ note.writer }}</p>
            <p class="text-lg font-bold"> {{ note.headline }}</p>
            <p @click="showModal = true" class="text-left p-3.5 text-sm"> {{ note.text }}</p>
        </div>
    </div>
</div>
</template>

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

const notes = ref([]);

notes.value = JSON.parse(localStorage.getItem('notes'));

    function deleteBtn(note){
        notes.value.splice(notes.value.indexOf(note), 1);
        localStorage.setItem('notes', JSON.stringify(notes.value));


        notes.value = JSON.parse(localStorage.getItem('notes'));
        console.log(notes.value)
    }

const showModal = ref(false)
</script>

更改后看起来像这样;我怎样才能让空白方块消失,并且在白色文本区域中将不透明度设置为 0,这样后面的文字就不会显示了。我试过 tailwind opacity 函数但没有用。

【问题讨论】:

    标签: vue.js click size tailwind-css v-for


    【解决方案1】:

    这是一种可用于条件渲染的方法。

    在不使用子组件处理v-loops时,您可以在循环中使用index,然后有条件地使用v-if="selectedIndex = index渲染它。

    在模态部分,你像这样添加v-if

    <div 
      v-if="showModal" 
      @click="showModal = false" 
      class="absolute w-screen h-screen bg-black opacity-75 z-10 flex cursor-pointer items-center justify-center"
    >
        <div 
          v-for="(note, index) in notes" 
          :key="note.id" 
          class="bg-white hover:border-red-400 p-4 rounded-full text-2xl font-bold"
        >
          <p v-if="selectedIndex === index">{{ note.text }}</p>
        </div>
    </div>
    

    然后在您渲染要单击的文本的地方,添加一个单击事件以在您单击具有相关索引的不同渲染内容时更新selectedIndex

    <div
      v-for="(note, index) in notes"
      :key="note.id"
      class="border-2 border-slate-100 rounded-3xl hover:border-red-400"
      @click="selectedIndex = index"
    > 
       <div 
          class="relative flex flex-col m-0 justify-around cursor-pointer"
       >
         ...
       </div>
    </div>
    

    然后在脚本标签中,只需添加 selectedIndex 的引用

    const selectedIndex = ref(null)
    

    笔记: 如果您的@click="showModal = true" 比 selectedIndex 更新快,您将获得“延迟”更新。要解决此问题,您需要将 @click="showModal = true" 转换为函数,其中包括 updateIndex 和此 showModal 重新分配。

    它看起来像这样

    <template>
    // other parts...
    <p @click="updateValues(index)" class="text-left p-3.5 text-sm"> {{ note.text }}</p>
    // other parts...
    </template>
    
    <script setup>
    // ... your other functions above
    
    const updateValues = (index) => {
      selectedIndex.value = index
      showModal.value = true
    }
    </script>
    

    无论如何,我认为最好将这两个项目(模态和呈现文本)拆分为不同的组件,这样您就可以更轻松地处理逻辑。

    【讨论】:

    • 非常感谢,现在它只显示点击的文本。我在问题的最后一张图片中添加了结果。我仍然看到一些小的空方块。你知道我怎样才能使不透明度为零吗?我尝试使用 tailwind opacity-0,但没有任何显示。提前致谢
    猜你喜欢
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多