【问题标题】:Vue js 2 hide shared componentVue js 2 隐藏共享组件
【发布时间】:2017-06-28 09:30:58
【问题描述】:
<template>
<div id="app" class="phone-viewport">
  <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic">
  <link rel="stylesheet" href="//fonts.googleapis.com/icon?family=Material+Icons">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">

  <router-view></router-view>
  <bottomBar v-bind:visibles='show' ></bottomBar>

</div>
</template>

<script>
export default {

  name: '',
  show: '',

  data () {
    return {
      visibles: [
        {name: 'Football', show: true},
        {name: 'Basketball', show: true},
        {name: 'Hockey', show: true},
        {name: 'VolleyBall', show: false},
        {name: 'Baseball', show: false},

      ]
    }
  }
}
</script>

我希望在 VolleyBall 和 Beisbol 上隐藏 bottomBar。 但我总是有这个错误“属性或方法“显示”没有在实例上定义,但在渲染期间被引用。确保在数据选项中声明反应数据属性。 "

<script>
export default {

  name: 'app',

  data () {
    return {}
  },
  computed: {
    hideBottom: function () {
      if (this.$router.path === '/baseball' || this.$router.path === '/volleyball') return false
      else { return true }
    }
  }
}

【问题讨论】:

  • 它对您有帮助还是来自其他人的支持?

标签: vue.js vuejs2 vue-component vue-router


【解决方案1】:
  1. 棒球
  2. 您正在调用不存在的方法 show,这就是您收到该错误的原因。
  3. 据我了解,您想在特定路线上隐藏该组件?
  4. 如果是这样,您需要创建computed 变量来确定它是否应该显示。例如:
computed: {
   toShowOrNotToShow: function () {
      if(this.$router.path === '/baseball' || this.$router.path === '/volleyball') return false;
      else
          return true;
    }
  }
  1. 随便用吧:&lt;bottomBar v-if='toShowOrNotToShow' &gt;&lt;/bottomBar&gt;

【讨论】:

  • 我尝试了你的代码,但它仍然显示bottomBar,我应该在脚本标签中声明一些方法吗?
  • 看起来你不知道计算是如何工作的。您需要将我的代码放入您的脚本标签中。看文档。vuejs.org/v2/guide/computed.html
  • 是的,我已将它添加到
  • 能否请加:mounted:mounted:function(){console.log(this.$router)}可能是你没有注入router
  • 我在计算属性后应用了“mounted:mounted:function () { console.log(this.$router) }”,但没有任何结果。你有安装的文档吗?
猜你喜欢
  • 2018-05-15
  • 2020-09-24
  • 2017-12-31
  • 1970-01-01
  • 2017-08-12
  • 2021-03-26
  • 2017-11-02
  • 1970-01-01
  • 2012-09-22
相关资源
最近更新 更多