Javascript Vue故事书意外标记(3:14)

Javascript Vue故事书意外标记(3:14),javascript,vue.js,storybook,Javascript,Vue.js,Storybook,我使用了以下文档来安装 <script> export default { props: { value: { required: true }, attrs: { default: () => ({}) }, handlers: { default: null }, formName: { required: true },

我使用了以下文档来安装

<script>
export default {
    props: {
        value: { required: true },
        attrs: {
            default: () => ({})
        },
        handlers: {
            default: null
        },
        formName: { required: true },
        name: { required: true },
        type: {
            default: 'text'
        },
        validator: {
            type: [Function],
            default: () => true
        }
    },
    data: () => ({
        hasError: false
    }),
    computed: {
        hasLabelSlot() {
            return !!this.$slots.label
        },
        isNumber() {
            return this.type === 'number'
        },
        isString() {
            return Object.keys(this.$attrs).includes('stringify')
        }
    },
    methods: {
        onInputChange(value) {
            const output = this.isNumber && !this.isString
                ? Number(value) || 0
                : value
            if (this.attrs.maxLength) {
                this.$emit('value', output.slice(0, this.attrs.maxLength))
                this.$forceUpdate()
            } else {
                this.$emit('value', output)
            }
        },
        validate({ id, form = {} }) {
            if (id === this.formName) {
                const formInput = this.name && form[this.name]
                const isValid = formInput ? this.validator(formInput) : this.validator(this.value)
                this.hasError = !isValid
                this.$emit('error', { name: this.name, hasError: this.hasError })
            }
        }
    },
    beforeMount() {
        this.$parent.$on('validate', this.validate)
    },
    beforeDestroy() {
        this.$parent.$off('validate', this.validate)
    }
}
</script>
<style lang="scss" scoped>

.input-field {
    label {
        width: 100%;
    }
    &-wrapper {
        position: relative;

        input {
            width: 100%;
        }
    }

    &-error {
        font-size: 12px;
        color: $input-field-error-color;
        padding: 10px 0;
    }
}
</style>
在我的
stories
文件夹中,我有一个
InputField.js
组件:

<script>
export default {
    props: {
        value: { required: true },
        attrs: {
            default: () => ({})
        },
        handlers: {
            default: null
        },
        formName: { required: true },
        name: { required: true },
        type: {
            default: 'text'
        },
        validator: {
            type: [Function],
            default: () => true
        }
    },
    data: () => ({
        hasError: false
    }),
    computed: {
        hasLabelSlot() {
            return !!this.$slots.label
        },
        isNumber() {
            return this.type === 'number'
        },
        isString() {
            return Object.keys(this.$attrs).includes('stringify')
        }
    },
    methods: {
        onInputChange(value) {
            const output = this.isNumber && !this.isString
                ? Number(value) || 0
                : value
            if (this.attrs.maxLength) {
                this.$emit('value', output.slice(0, this.attrs.maxLength))
                this.$forceUpdate()
            } else {
                this.$emit('value', output)
            }
        },
        validate({ id, form = {} }) {
            if (id === this.formName) {
                const formInput = this.name && form[this.name]
                const isValid = formInput ? this.validator(formInput) : this.validator(this.value)
                this.hasError = !isValid
                this.$emit('error', { name: this.name, hasError: this.hasError })
            }
        }
    },
    beforeMount() {
        this.$parent.$on('validate', this.validate)
    },
    beforeDestroy() {
        this.$parent.$off('validate', this.validate)
    }
}
</script>
<style lang="scss" scoped>

.input-field {
    label {
        width: 100%;
    }
    &-wrapper {
        position: relative;

        input {
            width: 100%;
        }
    }

    &-error {
        font-size: 12px;
        color: $input-field-error-color;
        padding: 10px 0;
    }
}
</style>
我的
1-Button.stories.js
看起来像:

<script>
export default {
    props: {
        value: { required: true },
        attrs: {
            default: () => ({})
        },
        handlers: {
            default: null
        },
        formName: { required: true },
        name: { required: true },
        type: {
            default: 'text'
        },
        validator: {
            type: [Function],
            default: () => true
        }
    },
    data: () => ({
        hasError: false
    }),
    computed: {
        hasLabelSlot() {
            return !!this.$slots.label
        },
        isNumber() {
            return this.type === 'number'
        },
        isString() {
            return Object.keys(this.$attrs).includes('stringify')
        }
    },
    methods: {
        onInputChange(value) {
            const output = this.isNumber && !this.isString
                ? Number(value) || 0
                : value
            if (this.attrs.maxLength) {
                this.$emit('value', output.slice(0, this.attrs.maxLength))
                this.$forceUpdate()
            } else {
                this.$emit('value', output)
            }
        },
        validate({ id, form = {} }) {
            if (id === this.formName) {
                const formInput = this.name && form[this.name]
                const isValid = formInput ? this.validator(formInput) : this.validator(this.value)
                this.hasError = !isValid
                this.$emit('error', { name: this.name, hasError: this.hasError })
            }
        }
    },
    beforeMount() {
        this.$parent.$on('validate', this.validate)
    },
    beforeDestroy() {
        this.$parent.$off('validate', this.validate)
    }
}
</script>
<style lang="scss" scoped>

.input-field {
    label {
        width: 100%;
    }
    &-wrapper {
        position: relative;

        input {
            width: 100%;
        }
    }

    &-error {
        font-size: 12px;
        color: $input-field-error-color;
        padding: 10px 0;
    }
}
</style>
import { action } from '@storybook/addon-actions';
import { linkTo } from '@storybook/addon-links';

import InputField from './InputField';

export const Input = () => ({
  components: { InputField },
});

你知道我做错了什么吗?

不应该
:for
v-for
?这很有帮助,谢谢你。但现在还有更多的问题。这意味着相邻的JSX元素必须被包装在一个封闭的标记中。即使是vue组件,您也需要JSX片段吗?为什么要将其放在js文件而不是JSX文件中?您的意思是
.vue
?我把它改成了那样,现在得到了一个不同的错误。我之所以选择
.js
是因为我在这里读到:
<script>
export default {
    props: {
        value: { required: true },
        attrs: {
            default: () => ({})
        },
        handlers: {
            default: null
        },
        formName: { required: true },
        name: { required: true },
        type: {
            default: 'text'
        },
        validator: {
            type: [Function],
            default: () => true
        }
    },
    data: () => ({
        hasError: false
    }),
    computed: {
        hasLabelSlot() {
            return !!this.$slots.label
        },
        isNumber() {
            return this.type === 'number'
        },
        isString() {
            return Object.keys(this.$attrs).includes('stringify')
        }
    },
    methods: {
        onInputChange(value) {
            const output = this.isNumber && !this.isString
                ? Number(value) || 0
                : value
            if (this.attrs.maxLength) {
                this.$emit('value', output.slice(0, this.attrs.maxLength))
                this.$forceUpdate()
            } else {
                this.$emit('value', output)
            }
        },
        validate({ id, form = {} }) {
            if (id === this.formName) {
                const formInput = this.name && form[this.name]
                const isValid = formInput ? this.validator(formInput) : this.validator(this.value)
                this.hasError = !isValid
                this.$emit('error', { name: this.name, hasError: this.hasError })
            }
        }
    },
    beforeMount() {
        this.$parent.$on('validate', this.validate)
    },
    beforeDestroy() {
        this.$parent.$off('validate', this.validate)
    }
}
</script>
<style lang="scss" scoped>

.input-field {
    label {
        width: 100%;
    }
    &-wrapper {
        position: relative;

        input {
            width: 100%;
        }
    }

    &-error {
        font-size: 12px;
        color: $input-field-error-color;
        padding: 10px 0;
    }
}
</style>