简体   繁体   English

vue js传递“top-level”布尔值不起作用,只嵌套

[英]vue js passing “top-level” Boolean not working, only nested

This is weird. 这很奇怪。 When I define the Boolean property showModal at the top level it is simply being ignored by vue.js. 当我在顶层定义布尔属性showModal时,它只是被vue.js忽略。 Here's what I am doing: 这就是我在做的事情:

//Component:
export default {
  props:['rating', 'showModal'],
  data: function data () {
    return {
      rating: this.rating,
      showModal: this.showModal
    };
  }
};

Invoking the view: 调用视图:

const sharedRating = {
  title: '',
  remark: ''
};

let showModal = false;

new Vue({
  el: '#rating-edit-container',
  data: {
    showModal: showModal,
    rating: sharedRating
  }
});

showModal = true;

Then both values are being passed to the component: 然后将两个值传递给组件:

  <rating-edit
    :rating="rating"
    :show-modal="showModal"></rating-edit>

But when I change the value of showModal nothing happens. 但是当我改变showModal的值时没有任何反应。

If I pass showModal inside the rating object and use that nested property everything works fine: 如果我在rating对象中传递showModal并使用该嵌套属性一切正常:

const sharedRating = {
  showModal: false,
  title: '',
  remark: ''
};

new Vue({
  el: '#rating-edit-container',
  data: {
    rating: sharedRating
  }
});

sharedRating.showModal = true;

Shouldn't "stand alone" boolean properties also be working in Vue or do they always need to be "wrapped"? 不应该“独立”布尔属性也可以在Vue中工作,还是总是需要“包裹”?

Well, first of all, I'm not sure what is happening in your component because you are naming a property and a data value with the same name. 好吧,首先,我不确定您的组件中发生了什么,因为您命名的属性和具有相同名称的数据值。 Without testing, I'm not sure which one wins, but it looks like it is the data property. 没有测试,我不确定哪一个获胜,但它看起来像是数据属性。 You should not name a property and a data value the same thing. 应该名称的属性和数据值同样的事情。 The property will always be available to you. 该物业将永远为您服务。

When you define your data function in a component, it's also important to remember that the data function is only ever called when the component is created . 在组件中定义数据函数时,记住只在创建组件时才调用数据函数也很重要。 Where you set 在哪里设置

showModal: this.showModal

(disregarding for a moment what I said above about this not being a good idea) showModal the data property is only ever set once . (暂时忽略我上面所说的关于这不是一个好主意)showModal 数据属性只设置一次 It will never be updated unless you update it inside the component. 除非您在组件内部更新,否则永远不会更新它。 It will not receive changes to the property showModal . 不会收到属性showModal更改。

If you wanted the component to get updates to showModal from the outside and for changes to showModal in the component be relfected outside the component, you need to handle this a little differently. 如果你想要的组件来获得更新showModal来自外部和变化showModal在组件的组件之外 relfected,你需要不同的方式处理这一点。

Vue components are composed in a props down, events up structure. Vue组件由props down,events up结构组成。

export default {
  props:['rating', 'showModal'],
  data: function data () {
    return {
      rating: this.rating,
    };
  }, 
  computed:{
    show:{
      get(){ return this.showModal; }
      set(v){ this.$emit('input', v) }
    }
  }
};

And then modify the way you use it in your template: 然后修改您在模板中使用它的方式:

rating-edit
  :rating="rating"
  v-model="showModal"></rating-edit>

Writing your component in this fashion, any update to showModal outside the component will be reflected inside the component as show , and any change to show inside the component will be emitted to the parent. 这种方式编写您的组件,任何更新showModal在组件将组件作为内部反射show ,任何改变show 里面的组件将被发射到父。 Then because v-model listens to input events, showModal in the parent will be updated with the new value. 然后因为v-model监听input事件,所以父级中的showModal将使用新值进行更新。

Now you may be asking at this point, but why does it work when showModal is a property of an object? 现在你可能会问这个问题,但是当showModal是一个对象的属性时,它为什么会起作用?

When you are passing the object down from the top level into your component, outside your Vue, your root Vue, and your component are all working with the same object. 当您将对象从顶层向下传递到组件中时,在Vue之外,根Vue和组件都在使用同一个对象。 Any change to the showModal property from any of those places will be reflected in all of those places. 任何这些地方对showModal属性的任何更改都将反映在所有这些地方。 If however, you were to update showModal by making a new sharedRating object, you would see similar behavior to what you see when you use a boolean. 但是, 如果您通过创建一个新的sharedRating对象来更新showModal ,您会看到与使用布尔值时所看到的行为类似的行为。 Properties in Vue are immutable which means that any change you make to a primitive javascript value (string, number, boolean, etc) will not be reflected outside the component, and in fact Vue will throw a warning about doing that if you are using the development version. Vue中的属性是不可变的 ,这意味着您对原始 javascript值(字符串,数字,布尔值等)所做的任何更改都不会反映在组件外部 ,事实上 ,如果您使用的话,Vue会发出警告开发版。 Objects and arrays, however, in javascript, are passed by reference. 但是,javascript中的对象和数组是通过引用传递的。 The reference is immutable, but the properties of the object and the contents of an array can be changed. 引用是不可变的,但可以更改对象的属性和数组的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM