• Vue Options / Data


    Options / Data

    data

    • Type: Object | Function

    • Restriction: Only accepts Function when used in a component definition.

    • Details:

      The data object for the Vue instance. Vue will recursively convert its properties into getter/setters to make it “reactive”. The object must be plain: native objects such as browser API objects and prototype properties are ignored. A rule of thumb is that data should just be data - it is not recommended to observe objects with their own stateful behavior.

      Once observed, you can no longer add reactive properties to the root data object. It is therefore recommended to declare all root-level reactive properties upfront, before creating the instance.

      After the instance is created, the original data object can be accessed as vm.$data. The Vue instance also proxies all the properties found on the data object, so vm.a will be equivalent to vm.$data.a.

      Properties that start with _ or $ will not be proxied on the Vue instance because they may conflict with Vue’s internal properties and API methods. You will have to access them as vm.$data._property.

      When defining a component, data must be declared as a function that returns the initial data object, because there will be many instances created using the same definition. If we use a plain object for data, that same object will be shared by reference across all instances created! By providing a data function, every time a new instance is created we can call it to return a fresh copy of the initial data.

      If required, a deep clone of the original object can be obtained by passing vm.$data through JSON.parse(JSON.stringify(...)).

    var data = { a: 1 }
    
    // direct instance creation
    var vm = new Vue({
      data: data
    })
    vm.a // => 1
    vm.$data === data // => true
    
    // must use function when in Vue.extend()
    var Component = Vue.extend({
      data: function () {
        return { a: 1 }
      }
    })
    • Note that if you use an arrow function with the data property, this won’t be the component’s instance, but you can still access the instance as the function’s first argument:

      data: vm => ({ a: vm.myProp })
    • See also: Reactivity in Depth

    props

      • Type: Array<string> | Object

      • Details:

        A list/hash of attributes that are exposed to accept data from the parent component. It has an Array-based simple syntax and an alternative Object-based syntax that allows advanced configurations such as type checking, custom validation and default values.

        With Object-based syntax, you can use following options:

        • type: can be one of the following native constructors: String, Number, Boolean, Array, Object, Date, Function, Symbol, any custom constructor function or an array of those. Will check if a prop has a given type, and will throw a warning if it doesn’t. More information on prop types.
        • default: any
          Specifies a default value for the prop. If the prop is not passed, this value will be used instead. Object or array defaults must be returned from a factory function.
        • required: Boolean
          Defines if the prop is required. In a non-production environment, a console warning will be thrown if this value is truthy and the prop is not passed.
        • validator: Function
          Custom validator function that takes the prop value as the sole argument. In a non-production environment, a console warning will be thrown if this function returns a falsy value (i.e. the validation fails). You can read more about prop validation here.
    // simple syntax
    Vue.component('props-demo-simple', {
      props: ['size', 'myMessage']
    })
    
    // object syntax with validation
    Vue.component('props-demo-advanced', {
      props: {
        // type check
        height: Number,
        // type check plus other validations
        age: {
          type: Number,
          default: 0,
          required: true,
          validator: function (value) {
            return value >= 0
          }
        }
      }
    })

    component data vs its props in vuejs

    Properties are meant to be propagated and managed from parent components, while data is the component internal state (which the component is responsible for).

    This concept is taken from React as far as i know, and it works pretty well. https://github.com/uberVU/react-guide/blob/master/props-vs-state.md

    computed

    • Type: { [key: string]: Function | { get: Function, set: Function } }

    • Details:

      Computed properties to be mixed into the Vue instance. All getters and setters have their this context automatically bound to the Vue instance.

      Note that if you use an arrow function with a computed property, this won’t be the component’s instance, but you can still access the instance as the function’s first argument:

      computed: {
        aDouble: vm => vm.a * 2
      }

      Computed properties are cached, and only re-computed on reactive dependency changes. Note that if a certain dependency is out of the instance’s scope (i.e. not reactive), the computed property will not be updated.

    var vm = new Vue({
      data: { a: 1 },
      computed: {
        // get only
        aDouble: function () {
          return this.a * 2
        },
        // both get and set
        aPlus: {
          get: function () {
            return this.a + 1
          },
          set: function (v) {
            this.a = v - 1
          }
        }
      }
    })
    vm.aPlus   // => 2
    vm.aPlus = 3
    vm.a       // => 2
    vm.aDouble // => 4

    watch

    • Type: { [key: string]: string | Function | Object | Array}

    • Details:

      An object where keys are expressions to watch and values are the corresponding callbacks. The value can also be a string of a method name, or an Object that contains additional options. The Vue instance will call $watch() for each entry in the object at instantiation.

    var vm = new Vue({
      data: {
        a: 1,
        b: 2,
        c: 3,
        d: 4,
        e: {
          f: {
            g: 5
          }
        }
      },
      watch: {
        a: function (val, oldVal) {
          console.log('new: %s, old: %s', val, oldVal)
        },
        // string method name
        b: 'someMethod',
        // the callback will be called whenever any of the watched object properties change regardless of their nested depth
        c: {
          handler: function (val, oldVal) { /* ... */ },
          deep: true
        },
        // the callback will be called immediately after the start of the observation
        d: {
          handler: 'someMethod',
          immediate: true
        },
        // you can pass array of callbacks, they will be called one-by-one
        e: [
          'handle1',
          function handle2 (val, oldVal) { /* ... */ },
          {
            handler: function handle3 (val, oldVal) { /* ... */ },
            /* ... */
          }
        ],
        // watch vm.e.f's value: {g: 5}
        'e.f': function (val, oldVal) { /* ... */ }
      }
    })
    vm.a = 2 // => new: 2, old: 1

    Note that you should not use an arrow function to define a watcher (e.g. searchQuery: newValue => this.updateAutocomplete(newValue)). The reason is arrow functions bind the parent context, so this will not be the Vue instance as you expect and this.updateAutocomplete will be undefined.

    这玩意有deep watch

  • 相关阅读:
    PHP 缓存技术
    redis雪崩
    【Redis】- 缓存击穿
    Memcache 与 Memcached 的区别
    数据库设计:范式与反范式
    Thinkphp5多数据库切换
    PHP 分布式集群中session共享问题以及session有效期的设置
    Nginx使用upstream实现动静分离
    rsync 服务快速部署手册
    tp5 为什么使用单例模式
  • 原文地址:https://www.cnblogs.com/chucklu/p/14241974.html
Copyright © 2020-2023  润新知