2.9 KiB
2.9 KiB
script-setup写法
第一部分
template
第二部分
script
一、import的顺序, 依次按照下面的顺序。
-
vue模块
import { } from 'vue';
-
vue相关类型
import type { } from 'vue';
-
vue-router模块
import { } from 'vue-router';
-
vue-router相关类型
import type { } from 'vue-router';
-
UI框架模块
import { } from 'naive-ui';
-
UI框架相关类型
import type { } from 'naive-ui';
-
第三方依赖
import BScroll from 'bscroll';
-
第三方依赖相关类型
import type { } from 'bscroll';
-
@/enum
import { } from '@/enum';
-
@/setting
import { } from '@/setting';
-
@/plugins
import { } from '@/plugins';
-
@/layouts
import { } from '@/layouts';
-
@/views
import { } from '@/views';
-
@/components
import { } from '@/components';
-
@/hooks
import { } from '@/hooks';
-
@/store
import { } from '@/store';
-
@/context
import { } from '@/context';
-
@/router
import { } from '@/router';
-
@/service
import { } from '@/service';
-
@/utils
import { } from '@/utils';
-
@/interface
import { } from '@/interface';
-
@/assets
import { } from '@/assets';
-
相对路径依赖
import { } from './components';
二、TS类型声明
interface Props {
/**姓名 */
name: string;
/**年龄 */
age?: number;
}
interface Emits {
/**
* 删除事件
* @param id - 删除项的id
*/
(e: 'delete', id: number): void;
}
三、defineProps、defineEmits、withDefaults
- 定义属性,如:
const props = withDefaults(defineProps<Props>(), {
age: 24
});
其中name是必须的属性,age是可选属性,通过withDefaults添加默认值
- 定义emit事件
const emit = defineEmits<Emits>();
四、响应式use函数
有些use函数需要传入响应式的变量参数时,则书写在声明的变量下面。
const router = useRouter();
const route = useRoute();
/**dom引用 */
const domRef = ref<HTMLElement | null>(null);
const { height: domRefHeight } = useElementSize(domRef); //获取domRef的响应式高度