Home > Mobile >  Why the text isn’t updated in Vue3?
Why the text isn’t updated in Vue3?

Time:07-14

I’m trying to display a name dynamically, but I get the same name forEach element. What I’m trying to do is:

<template>
  <div >
    <div
      
      v-for="project in visibleProjects"
      :key="project.id"
      :id="project.id"
    >
      <div >
        <img
          :src="project.imgUrl"
          alt="Project Image"
          width="570"
          height="320"
          loading="lazy"
        />
      </div>
      <div >
        <h3>{{ project.name }}</h3>
        <p>
          {{ project.description }}
        </p>
        <a
          :href="project.link"
          target="_blank"
          
        >
          {{ displayNameButton }}
        </a>
        <hr  />
      </div>
    </div>
    <button
      v-if="showMoreProjectsButton"
      
      @click="loadMoreProjects"
    >
      show more projects
    </button>
  </div>
</template>

On the I'm trying to display a name dynamically, and all the time the same name is displayed, but I want to display the name based on the computed property that I wrote below.

Here is the visibleProjects:

const visibleProjects = computed(() => {
  return storeProjects.projects.slice(0, maxProjectsShown.value);
});

I’m trying to iterate through an array of objects from the store like:

const displayNameButton = computed(() => {
  const isObjPresent = storeProjects.projects.find((o => o.wordpress === 'yes')).wordpress;
  console.log(isObjPresent);
  if (isObjPresent === 'yes') return 'See Website';
  else if (!isObjPresent) return 'See code';
})

The array of objects from the store is:

import { defineStore } from 'pinia';
import { v4 as uuidv4 } from 'uuid';

export const useProjectsStore = defineStore({
  id: 'projects',
  state: () => {
    return {
      projects: [
        {
          id: uuidv4(),
          imgUrl: lightImg,
          name: 'use this',
          description:
            'track of this',
          wordpress: false,
        },
        {
          id: uuidv4(),
          imgUrl: recogn,
          name: 'deep lear',
          description:
            'I tried my best',
          wordpress: ‘yes’,
        },
        ... 
        {},
        {},
      ],
    };
  },
});

CodePudding user response:

So the problem is with your computed property. It will always return the same value because there is no input based on which the function can determine which string should it returns. Based on the code you already have I think you should write a method that will return desired string.

const displayNameButton = (project) => {
  return (project.wordpress === 'yes') ? 'See Website' : 'See code';
})

and in the template

    <a
      :href="project.link"
      target="_blank"
      
    >
      {{ displayNameButton(project) }}
    </a>

OR you can modify your visibleProjects:

const visibleProjects = computed(() => {
  return storeProjects.projects.slice(0, maxProjectsShown.value).map((e) => {      
    const project = {...e};
    project.wordpress = (project.wordpress === 'yes') ? 'See Website' : 'See code';
    return project;
  });
});

and in the template

    <a
      :href="project.link"
      target="_blank"
      
    >
      {{ project.wordpress }}
    </a>
  • Related