【问题标题】:Vuejs: method with v-if not workingVuejs:带有v-if的方法不起作用
【发布时间】:2017-11-27 10:43:32
【问题描述】:

我认为这可能是某个地方的错字,但找不到问题。我有这个 Vuejs 模板,如果我删除 v-if 验证,它就可以很好地呈现。但是,当我使用它时,它根本不会渲染任何东西。已经在 return true 和 return false 中放置了一个调试器,并且逻辑测试只返回一次 true,正如预期的那样。谁能发现我做错了什么?

template: `
  <div class="workbench container">
    <ul class="collapsible popout" data-collapsible="expandable">
      <collapsible-cards
        v-for="tipo, index in tiposCollapsibles"
        v-if="mostraApenasPerfilEspecificado(perfil, tipo)"
        v-bind:key=index
        v-bind:dados="tipo"
      >
      </collapsible-cards>
    </ul>
  </div>`,

mounted: function() {
  for (key in this.tiposCollapsibles) {
    if (this.tiposCollapsibles[key].perfisQuePodemVer.indexOf(this.perfil) >= 0) {
        this.queryTeleconsultorias(key);
    }
  }
},

methods: {
  mostraApenasPerfilEspecificado(perfil, tipo) {
    tipo['perfisQuePodemVer'].forEach(function(value) {
      if (perfil === value) {
        return true;
      }
    });
    return false;
  },
  ...

更新:对于遇到同样问题的任何人,我最终都使用了计算属性,而不是方法本身。显示/隐藏元素的 v-if/-v-show 行为已移至计算属性。最后我不确定这是否是 Vuejs 的问题。这是工作代码:

template: `
  <div class="workbench container">
    <ul class="collapsible popout" data-collapsible="expandable">
      <collapsible-cards
        v-if="showTipoCollapsibles[index]"
        v-for="tipo, index in tiposCollapsibles"
        v-bind:key="index"
        v-bind:object="tipo"
      >
      </collapsible-cards>
    </ul>
  </div>`,

mounted: function() {
  this.executeQuery(this.perfil);
},

computed: {
  showTipoCollapsibles: function() {
    let perfisVisiveis = {};
    for (tipo in this.tiposCollapsibles) {
      perfisVisiveis[tipo] = this.tiposCollapsibles[tipo].enabledForProfiles.includes(this.perfil);
    }
    return perfisVisiveis;
  },
},

methods: {
  executeQuery: function(value) {
    if (value === 'monitor') {
      var query = gql`query {
        entrada(user: "${this.user}") {
          id
          chamadaOriginal {
            datahoraAbertura
            solicitante {
              usuario {
                nome
              }
            }
          }
          ...

【问题讨论】:

  • 检查错字v-bind:key=index,应该是v-bind:key="index"

标签: javascript vuejs2


【解决方案1】:

v-if 更改为v-show

v-show="mostraApenasPerfilEspecificado(perfil, tipo)"

您也可以使用templatev-if 用作外部子组件

template: `
  <div class="workbench container">
    <ul class="collapsible popout" data-collapsible="expandable">
      <template v-for="(tipo, index) in tiposCollapsibles">
        <collapsible-cards
          v-if="mostraApenasPerfilEspecificado(perfil, tipo)"
          v-bind:key="index"
          v-bind:dados="tipo">
        </collapsible-cards>
      </template>
    </ul>
  </div>`,

如果不行,分享现场演示

【讨论】:

  • 这会有什么不同?
  • 因为v-for在同一个标​​签中先执行然后v-if/v-show,因为v-if没有在浏览器中渲染任何东西它不会工作。 v-show 只会显示/隐藏,但会按预期工作并将元素保留在浏览器中。
  • yup v-show 切换 css 显示属性,v-if 切换是否渲染 DOM 元素。但是如果v-if='true' 它应该正确渲染
  • 添加模板标签无效。在 JS 控制台中得到了 tipo 未定义,因为 tipo 来自 v-for 值。
  • @Vini.g.fer :尝试将v-if 反转为v-for,作为答案的更新
【解决方案2】:

在 Bootstrap-Vue 中似乎存在类似的错误,其中v-if 仅适用于非 Bootstrap 元素。

其他部分相同的代码,当this.error = true时,该元素不会出现:

<b-alert v-if="error" variant="danger">Failed!</b-alert>

但这会:

<div v-if="error">Failed!</div>

【讨论】:

  • 看来这是设计使然。为了控制 Bootstrap-Vue 元素的显示/隐藏,有一个属性 show 接受一个布尔值。默认情况下,它是隐藏的(隐式show=false)。要切换它,你应该例如。 &lt;b-alert :show="myFunc"&gt; 其中 myFunc() 返回一个布尔值。
猜你喜欢
  • 1970-01-01
  • 2017-12-21
  • 2020-11-15
  • 2023-03-06
  • 2019-09-26
  • 2018-01-07
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多