Home > Web Front-end > Front-end Q&A > How to pass boolean value directly in vue

How to pass boolean value directly in vue

PHPz
Release: 2023-04-26 17:50:12
Original
1889 people have browsed it

Vue is a popular JavaScript framework for building interactive, modern web applications. Vue is easy to learn, efficient and flexible, allowing developers to quickly build high-performance single-page applications. This article will introduce how Vue passes boolean values ​​directly.

In the Vue component, we can receive the passed data through the props attribute of the component. In the parent component, we can pass data to the child component through the v-bind directive. Normally, the way we pass Boolean type values ​​to subcomponents is as follows:

<template>
  <div>
    <child-component :is-true="true"></child-component>
  </div>
</template>
Copy after login

The v-bind directive is used here to pass the value to the subcomponent, and the subcomponent can receive this through props Value:

Vue.component('child-component', {
  props: ['isTrue'],
  template: '<div>{{ isTrue }}</div>'
});
Copy after login

In this example, we explicitly pass a Boolean value to the subcomponent, and the subcomponent can perform corresponding processing based on this value.

However, in some cases, we may want to pass an expression directly to the child component, and the result of this expression is a Boolean value. For example:

<template>
  <div>
    <child-component :is-true="foo === &#39;bar&#39;"></child-component>
  </div>
</template>
Copy after login

In this example, we hope to determine whether the value of the foo variable is equal to 'bar', and if so, pass true to the child component. However, in Vue, this way of writing is not supported. If written like above, the child component will receive a string 'foo === 'bar' instead of a boolean value.

One way to solve this problem is to use computed properties in the parent component:

<template>
  <div>
    <child-component :is-true="isFooBar"></child-component>
  </div>
</template>

<script>
export default {
  data() {
    return {
      foo: 'bar'
    };
  },
  computed: {
    isFooBar() {
      return this.foo === 'bar';
    }
  }
};
</script>
Copy after login

In this example, we define a computed property isFooBar, which is based on the value of foo To determine whether it is equal to 'bar', if so, return true, otherwise return false. Then, we pass this calculated property to the child component in the template, and the child component can receive a Boolean value.

Although this method can solve the problem, it adds extra code and may not be the simplest solution. Fortunately, Vue provides an easier way to pass boolean values ​​directly. We only need to add a unary operator before the Boolean expression to coerce it into a Boolean value:

<template>
  <div>
    <child-component :is-true="+foo === +bar"></child-component>
  </div>
</template>
Copy after login

In this example, we use the unary operator to coerce foo and bar into Numeric type before comparing. The comparison result will be a Boolean value, which can be passed directly to the subcomponent, and the subcomponent can receive a Boolean value.

To summarize, Boolean values ​​can be passed directly in Vue, but it should be noted that if we pass an expression, we need to use a unary operator to force it to a Boolean value. In this way, we can more easily operate Boolean type data in Vue.

The above is the detailed content of How to pass boolean value directly in vue. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template