无法通过vue.js中的其他值更改选项状态数据值

发布于 2025-02-07 22:44:22 字数 1501 浏览 1 评论 0原文

我正在尝试将结果值在其他值之间进行true/false之间。我希望设置结果为true,如果所有其他值都不为空。

我认为它看起来不错,但是结果属性一直返回真实。因此,我无法隐藏结果< p>标签。

<script src="https://unpkg.com/[email protected]/dist/vue.global.prod.js"></script>

<div id="app">
  <label>
    Name :
    <input type="text" v-model="name" />
  </label>

  <label>
    Size :
    <input type="text" v-model="size" />
  </label>

  <label>
    Quantity :
    <input type="text" v-model="quantity" />
  </label>

  <p v-if="result">Summary: {{ name }} - {{ size }} - {{ quantity }}</p>
</div>

<script>
  const { createApp } = Vue;

  createApp({
    data() {
      return {
        name: '',
        size: '',
        quantity: '',
        result: this.name !== '' && this.size !== '' && this.quantity !== '',
      };
    },
  }).mount('#app');
</script>

<style>
  #app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: left;
    color: #2c3e50;
    margin-top: 60px;
  }

  label {
    display: block;
    margin: 0 0 10px;
  }

  input {
    display: block;
  }
</style>

I am trying to change result value between true/false by other values. I want set result to true, if all other values are not empty.

I think it looks like good, but result property returns true all time. So i can not hide result <p> tag.

<script src="https://unpkg.com/[email protected]/dist/vue.global.prod.js"></script>

<div id="app">
  <label>
    Name :
    <input type="text" v-model="name" />
  </label>

  <label>
    Size :
    <input type="text" v-model="size" />
  </label>

  <label>
    Quantity :
    <input type="text" v-model="quantity" />
  </label>

  <p v-if="result">Summary: {{ name }} - {{ size }} - {{ quantity }}</p>
</div>

<script>
  const { createApp } = Vue;

  createApp({
    data() {
      return {
        name: '',
        size: '',
        quantity: '',
        result: this.name !== '' && this.size !== '' && this.quantity !== '',
      };
    },
  }).mount('#app');
</script>

<style>
  #app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: left;
    color: #2c3e50;
    margin-top: 60px;
  }

  label {
    display: block;
    margin: 0 0 10px;
  }

  input {
    display: block;
  }
</style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2025-02-14 22:44:22

在这种情况下,您应该将结果作为计算属性:

  createApp({
    data() {
      return {
        name: '',
        size: '',
        quantity: '',
        
      };
    },
computed:{
  result(){
    return this.name !== '' && this.size !== '' && this.quantity !== ''
  }

}
  }).mount('#app');

In this case you should defing result as a computed property :

  createApp({
    data() {
      return {
        name: '',
        size: '',
        quantity: '',
        
      };
    },
computed:{
  result(){
    return this.name !== '' && this.size !== '' && this.quantity !== ''
  }

}
  }).mount('#app');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文