admin管理员组

文章数量:1279119

Code:

<v-btn large color="green"  @click="function">
   <v-icon>star</v-icon> Add
</v-btn>

Is there a solution in Vue or is it also possible via JavaScript?

Code:

<v-btn large color="green"  @click="function">
   <v-icon>star</v-icon> Add
</v-btn>

Is there a solution in Vue or is it also possible via JavaScript?

Share Improve this question edited May 12, 2019 at 17:41 Dan Is Fiddling By Firelight 6,06118 gold badges80 silver badges131 bronze badges asked Jul 1, 2018 at 19:47 lannenerelannenere 691 gold badge2 silver badges5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

You can hide a button using the vue onClick-event v-on:click.

v-on:click="isHidden = true"

The attribute isHidden can be set to "true" so that the text or a button gets invisible if v-if="!isHidden" is added to the element of your choice.

Have a look at this easy snippet:

var myApp = new Vue({
  el: '#myApp',
  data: {
    isHidden: false
  }
})
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vue.js"></script>

<div id="myApp">
  <button v-on:click="isHidden = true">Hide text</button>
  <button v-on:click="isHidden = !isHidden">Toggle text</button>

  <h1 v-if="!isHidden">Hide me</h1>
</div>

Hide the button onClick is possible using this code:

var myApp = new Vue({
  el: '#myApp',
  data: {
    isHidden: false
  }
})
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vue.js"></script>

<div id="myApp">
  <button v-if="!isHidden" v-on:click="isHidden = true">Hide text</button>
</div>

<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vue.js"></script>
        
<div id="app">
  <button v-if="btn" @click="toggle">Press to Hide This Button</button>
</div>

Script

var app = new Vue({
  el: '#app',
  data: {
    btn: true
  },
  methods: {
    toggle() {
      this.btn = false;
    }
  }
})

something like this should work. if btn is true, the button will be displayed. if the button is pressed, the toggle function is triggered, btn is changed to false. the button is only shown if btn is true.

what you can do is to use css to add a class to the element. Front there you can now hide the element using css rules.

an example can be passing the state of the element (that is to hide or to show as an attribute to your ponent when clicked)

eg

<v-btn large color="green"  @click="function">
    <v-icon>star</v-icon> Add
</v-btn>

to

<v-btn large color="green"  @click="function" v-bind"hide = true">
     <v-icon>star</v-icon> Add
</v-btn>

then you can now use the variable in your vue ponent to know if you need to show or hide. That's all

本文标签: javascriptHow to hide a button in Vue after it got clickedStack Overflow