使用`v-if`在状态更改后隐藏元素。

huangapple go评论83阅读模式
英文:

Hide element with v-if after a state change

问题

这是组件代码的翻译部分:

<template>
  <div class="tile-content">
    <router-link :to="{ name: 'anime', params: { slug: slug } }">
      <div class="overlay"></div>
      <figure class="image is-16by9">
        <img :src="cover || defaultCover">
      </figure>
      <div class="name">
        <h3>{{ name }}</h3>
      </div>
      <div class="bookmark" v-if="isAuthenticated">
        <div v-if="isBookmarked" @click.prevent="unBookmark">
          <b-icon icon="star"></b-icon>
        </div>
        <div v-else @click.prevent="addBookmark">
          <b-icon icon="star-outline"></b-icon>
        </div>
      </div>
    </router-link>
  </div>
</template>

请注意,代码中的HTML标签和属性名称保持不变。

如果您需要进一步的帮助或翻译其他部分,请告诉我。

英文:

I have been crushing my head for 2 days with this, but I think I am not understanding the reactivity thing yet.

Here is the component:

&lt;template&gt;
  &lt;div class=&quot;tile-content&quot;&gt;
    &lt;router-link :to=&quot;{ name: &#39;anime&#39;, params: { slug: slug } }&quot;&gt;
      &lt;div class=&quot;overlay&quot;&gt;&lt;/div&gt;
      &lt;figure class=&quot;image is-16by9&quot;&gt;
        &lt;img :src=&quot;cover || defaultCover&quot;&gt;
      &lt;/figure&gt;
      &lt;div class=&quot;name&quot;&gt;
        &lt;h3&gt;{{ name }}&lt;/h3&gt;
      &lt;/div&gt;
      &lt;div class=&quot;bookmark&quot; v-if=&quot;isAuthenticated&quot;&gt;
        &lt;div v-if=&quot;isBookmarked&quot; @click.prevent=&quot;unBookmark&quot;&gt;
          &lt;b-icon icon=&quot;star&quot;&gt;&lt;/b-icon&gt;
        &lt;/div&gt;
        &lt;div v-else @click.prevent=&quot;addBookmark&quot;&gt;
          &lt;b-icon icon=&quot;star-outline&quot;&gt;&lt;/b-icon&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/router-link&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script lang=&quot;ts&quot;&gt;
import { Component, Vue, Prop } from &#39;vue-property-decorator&#39;;
import { mapState } from &#39;vuex&#39;;
import UserModule, { IUserState } from &#39;@/app/account/store&#39;;

@Component({
  computed: {
    ...mapState&lt;IUserState, any&gt;(&#39;User&#39;, {
      isAuthenticated: (state: IUserState) =&gt; !!state.account.token,
      bookmarks: (state: IUserState) =&gt; state.bookmarks,
    }),
    isBookmarked: function() {
      this.bookmarks.has(this.slug);
    }
  }
})
export default class Tile extends Vue {
  @Prop() private name!: string;
  @Prop() private slug!: string;
  @Prop() private cover!: URL;
  private isAuthenticated!: boolean;
  private isBookmarked!: boolean;
  private bookmarks!: Set&lt;string&gt;;

  get defaultCover() {
    return require(&#39;@/assets/default-cover.jpg&#39;);
  }

  private async addBookmark() {
    UserModule.AddBookmark(this.slug);
  }

  private async unBookmark() {
    UserModule.RemoveBookmark(this.slug);
  }
}
&lt;/script&gt;

&lt;style lang=&quot;scss&quot; scoped&gt;
&lt;/style&gt;

What I want to accomplish is this:

GIVEN User with bookmarks [A, B, C] (each of them is a "slug")
WHEN User clicks star on B
THEN unBookmark will be triggered removing B from state.bookmarks
AND star will change to start-outline

My problem is that bookmarks is a computed variable and slug is a prop, and I can't seem to find a way of comparing them every time the state changes.

答案1

得分: 2

刚刚发现,在Vue中Set类型尚不是响应式的(尚未实现)

https://github.com/vuejs/vue/issues/2410#issuecomment-434990853

这计划在Vue 3中实现。

英文:

Just found that Set type is not reactive in Vue (yet)

https://github.com/vuejs/vue/issues/2410#issuecomment-434990853

It is planned for Vue 3.

答案2

得分: 1

你可以使用一个 watcher 链接

同时你可能会对生命周期钩子感兴趣:beforeUpdate(更新前),updated(已更新)。

英文:

You can use a watcher https://v2.vuejs.org/v2/guide/computed.html

Also you might be interested in lifecycle hooks : beforeUpdate, updated .

huangapple
  • 本文由 发表于 2020年1月3日 13:38:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定