Home >Web Front-end >Front-end Q&A >What is the instruction to hide elements in vue

What is the instruction to hide elements in vue

青灯夜游
青灯夜游Original
2022-12-15 10:37:063459browse

The command to hide elements in vue is "v-show". The v-show instruction determines whether an element is displayed or hidden based on the true or false value of the expression. The syntax is "v-show="expression"". The v-show instruction controls the display and hiding state of the element by changing the CSS display attribute of the element. The content of the expression following the instruction will eventually be parsed into a Boolean value. When the value is true (true), the element is displayed, and when the value is false (false) ) when the element is hidden.

What is the instruction to hide elements in vue

The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.

Vue.js provides a v-show directive, which determines whether to display or hide an element based on the true or false value of the following expression.

The v-show command controls the display and hidden state of the element by changing the display attribute of the element's css.

v-show instruction introduction

The v-show instruction needs to be followed by a Boolean variable or Boolean expression. When the value is true Shows the element when the value is false and hides the element when the value is false.

<标签 v-show="true/false"></标签>

<!--true:显示   false:隐藏-->

Example:

<div id="app">
    <p v-show="flag == &#39;显示&#39;">Vue 课程</p>
</div>
<script>
    var app = new Vue({
        el: &#39;#app&#39;,
        data: {
            flag: &#39;隐藏&#39;
        }
    });
</script>

When the flag is set to 'hide', "Vue Course" will not be displayed; when the flag is set to 'show', "Vue Course" will be displayed Vue Courses".

In fact, when the flag is set to 'hidden', the css attribute display of the p element is set to none, so the element is hidden.

The rendered code is as follows:

<p style="display: none;">Vue 课程</p>

v-show code example

The following are html and js code examples

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1.0">
		<title>v-show指令</title>
	</head>
	<body>
		<div id="app">
			<input type="button" value="切换显示" @click="changeIsShow" />
			<p v-show="isShow">不装了,我摊牌了,没错你要找的就是我</p>
		</div>
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		<script>
			var  app = new Vue({
				el:"#app",
				data:{
					isShow:false
				},
				methods:{
					changeIsShow(){
						this.isShow = !this.isShow
					}
					
				}
			})
		</script>
	</body>
</html>

The code after rendering is as follows

<p style="display: none;">不装了,我摊牌了,没错你要找的就是我</p>

The running result of the code is as follows:

What is the instruction to hide elements in vue

Summary

  • The function of the v-show command is to switch the display state of the element based on the true or false value

  • The principle is to modify the element CSS property (display) determines whether to display or hide

  • The content following the directive will eventually be parsed into a Boolean value

  • The value is true (true ) when the element is displayed, and when the value is false (false), the element is hidden

  • After the data changes, the display status of the corresponding element will also be updated synchronously

[Related recommendations: vuejs video tutorial, web front-end development]

The above is the detailed content of What is the instruction to hide elements in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn