报错<script setup> cannot contain ES module exports vue/noexportinscriptsetup

vue3-jsonschemaform课程中StringField.vue照着打报错
代码如下

<template&gt;
  <input type="text" :value="value" @input="handleChange" /&gt;
</template&gt;
`<script lang="ts" setup="props"&gt;
import { ref } from 'vue'
import { FiledPropsDfine, Schema } from '../types'
export default {
  props: FiledPropsDfine,
}
declare const props: {
  // 向ts声明props定义
  value: any
  onChange: (v: string) => void
  schema: Schema
}

export const handleChange = (e: any) => {
  console.log(e)
  props.onChange(e.target.value)
}
</script>

修改代码如下

<template>
  <input type="text" :value="value" @input="handleChange" />
</template>
`<script lang="ts" setup="props">
import { FiledPropsDfine, Schema } from '../types'
declare const props: FiledPropsDfine &amp; {
  // 向ts声明props定义
  value: any
  onChange: (v: string) => void
  schema: Schema
}
const handleChange = (e: any) => {
  console.log(e)
  props.onChange(e.target.value)
}
</script>

type.ts文件代码片段如下

import { SchemaRefs } from 'ajv/dist/compile'
import { PropType } from 'vue'
export enum SchemaTypes {
  'NUMBER' = 'number',
  'INTEGER' = 'intrger',
  'STRING' = 'string',
  'OBJECT' = 'object',
  'ARRSY' = 'array',
  'BOOLEAN' = 'boolean',
}

type SchemaRef = { $ref: string } // 预先定义 可以使用$ref引用schema

// type Schema = any
export interface Schema {
  type: SchemaTypes | string // 加上string有利于类型校验 要不然只能用SchemaTypes.NUMBER来使用类型
  const?: any
  format?: string
  default?: any
  properties?: {
    [key: string]: Schema | { $ref: string }
  }
  items?: Schema | Schema[] | SchemaRefs
  dependencies?: {
    [key: string]: string[] | Schema | SchemaRef
  }
  oneOf?: Schema[]
  anyOf?: Schema[]
  allOf?: Schema[]
  //   vjsf?: VueJsonSchemaConfig
  required?: string[]
  enum?: any[]
  enumKeyValue?: any[]
  additionalProperties?: any
  additionalItems?: Schema
}

export const FiledPropsDfine = {
  schema: {
    type: Object as PropType<Schema>,
    required: true,
  },
  value: {
    required: true,
  },
  onChange: {
    type: Function as PropType<(v: any) => void>,
    required: true,
  },
  rootSchema: {
    type: Object as PropType<Schema>,
    required: true,
  },
} as const

主要问题就是说script标签中加上setup,代码块中不能再出现export default关键字,将该部分代码

export default {
  props: FiledPropsDfine,
}
declare const props: {
  // 向ts声明props的定义
  value: any
  onChange: (v: string) => void
  schema: Schema
}

修改为:

declare const props: FiledPropsDfine &amp; {
  // 向ts声明props的定义
  value: any
  onChange: (v: string) => void
  schema: Schema
}

原文地址:https://blog.csdn.net/weixin_44738844/article/details/129255140

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_19023.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注