javascript - Disable a component in a specific case in Vue.js - Stack Overflow

I looked for how to do it but I did not find an answer to my problem anywhere.Let me explain, I have my

I looked for how to do it but I did not find an answer to my problem anywhere.

Let me explain, I have my page in which I load a ponent I created 'date-picker'. I need it in most of the ponents I will load in the router-view. My problem is that I would, if possible, disable it when I load a specific ponent in the router-view.

<main>
  <date-picker></date-picker>
  <router-view></router-view>
</main> 

Is there a solution, other than nesting my 'date-picker' ponent in the ponents they use? I use Vuex if it helps.

Thanks in advance.

I looked for how to do it but I did not find an answer to my problem anywhere.

Let me explain, I have my page in which I load a ponent I created 'date-picker'. I need it in most of the ponents I will load in the router-view. My problem is that I would, if possible, disable it when I load a specific ponent in the router-view.

<main>
  <date-picker></date-picker>
  <router-view></router-view>
</main> 

Is there a solution, other than nesting my 'date-picker' ponent in the ponents they use? I use Vuex if it helps.

Thanks in advance.

Share Improve this question asked Jul 18, 2018 at 10:04 Benjamin LandryBenjamin Landry 31 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

One way of doing is to set up a meta field requiresDatePicker: true on the routes you want to show the datepicker

const router = new VueRouter({
  routes: [
    {
      path: "/foo",
      ponent: Foo,
      meta: { requiresDatePicker: true }
    },
    {
      path: "/bar",
      ponent: Bar,
      meta: { requiresDatePicker: false }
    }
  ]
});

Then use the meta property on the route object to check if the particular route has to render the datepicker or not

<main>
  <date-picker v-if="$route.meta.requiresDatePicker"></date-picker>
  <router-view></router-view>
</main> 

credits to @Konrad K for mentioning out in the ments

In your vuex, add a new state. Let's say, showDatePicker:

{
    state: {
        showDatePicker: false
    },
    mutation: {
        showDatePicker(state, show) {
            state.showDatePicker = show;
        }
    }
}

Then in your ponent, add a puted property for referencing the state:

import { mapState } from 'vuex'
...
puted: {
    ...mapState(['showDatePicker'])
}

then on the date-picker ponent add a condition:

 <date-picker v-if="showDatePicker"></date-picker>

then on the mounted callback on each page ponents, you can just toggle the value depending if you want to show the date picker or not:

mounted() {

    //show
    this.$store.mit('showDatePicker', true);

    //hide
    this.$store.mit('showDatePicker', false);

}

The easiest way is to use window.location.href and check for your route there. Then add a v-if on your ponent. Something like this:

<main>
  <date-picker v-if = "enableDatePicker"></date-picker>
  <router-view></router-view>
</main>

<script>
export default {
    puted: {
        enableDatePicker: {
              return (window.location.href === myroute)
         }
    }
}
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744358983a4570375.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信