项目作者: skyrpex

项目描述 :
Vue mixin that maps props to local data
高级语言: JavaScript
项目地址: git://github.com/skyrpex/props-to-local.git
创建时间: 2016-12-01T14:53:32Z
项目社区:https://github.com/skyrpex/props-to-local

开源协议:MIT License

下载


Props To Local

CircleCI AppVeyor Build status TravisCI Build status

Works with:

Vue 2

This mixin will sync down properties to local data. This allows defining a property that can be changed within the component (using v-model, for example).

A case of use is creating a checkbox component that that can be correctly toggled without passing down a value through a prop.

Installation

npm install @skyrpex/props-to-local

Usage

  1. <template>
  2. <input type="checkbox" v-model="local.value" @change="$emit('input', $event.target.checked)">
  3. </template>
  4. <script>
  5. import propsToLocal from '@skyrpex/props-to-local';
  6. // In this example, a 'value' prop is given to propsToLocal.
  7. export default {
  8. mixins: [
  9. propsToLocal({
  10. // Normal props here
  11. value: {
  12. type: Boolean,
  13. default: false,
  14. },
  15. }),
  16. ],
  17. };
  18. </script>

The above example will generate the following component:

  1. export default {
  2. props: {
  3. value: {
  4. type: Boolean,
  5. default: false,
  6. },
  7. },
  8. data() {
  9. return {
  10. local: {
  11. // Will default to false, as stated above.
  12. value: this.value,
  13. },
  14. };
  15. },
  16. };

Options

  1. <script>
  2. import { identity } from 'lodash';
  3. import propsToLocal '@skyrpex/props-to-local';
  4. export default {
  5. mixins: [
  6. propsToLocal({
  7. value: {
  8. type: Boolean,
  9. default: false,
  10. // Watch prop changes deeply (defaults to false).
  11. deep: false,
  12. // Format props before overwriting local values (defaults to Lodash.Identity).
  13. format: identity,
  14. },
  15. }),
  16. ],
  17. };
  18. </script>