Vue的一些经典写法

1、利用继承创建公共类,调用时实例化并使用

重点是:复用实例化方法

set-account-modal.vue

/*
**	一个普通的vue组件选项对象
*/
<template>
    <mtd-modal
        width="450px"
        :visible="visible" :title="title" closable
        @input="handleClose">
        <div class="content">
            <mtd-form ref="form" :rules="rules" :model="form">
                <mtd-form-item required prop="value" label="值">
                    <mtd-textarea
                        placeholder="描述信息"
                        clearable
                        v-model="form.value"
                        style="width: 260px;"
                        rows="3" maxlength="2048">
                    </mtd-textarea>
                </mtd-form-item>
            </mtd-form>
        </div>
        <div slot="footer">
            <mtd-button
                @click="handleClose">取消</mtd-button>
        </div>
    </mtd-modal>
</template>
<script>
import { setConfig } from '@/api/api';

export default {
    name: 'SetConfigModal',
    data () {
        return {
            loading: false,
            visible: false,
            type: 'add',
            title: '账号设置',
            name: '',
            value: '',
            clusterName: '',
            // 呼出该modal的tablemodule组件,用于处理modal与table的交互
            tableVm: null,

            form: {
                name: '',
                value: ''
            },
            rules: {
            }
        };
    }
};
</script>
<style scoped lang="postcss">
</style>

popModal.js

/*
** 将公共组件类,通过data实例化成不同的根实例。
*/
import Vue from 'vue';

const popModal = (modalConstructor, options) => {
    options = options || {};
    const instance = new modalConstructor({
        el: document.createElement('div'),
        data: options
    });
    document.body.appendChild(instance.$el);
    Vue.nextTick(() => {
        instance.visible = true;
    });
    return instance;
};

export default popModal;

*.vue

/*
**	
*/
import setAccountModal from './set-account-modal.vue';// 组件选项对象
import popModal from './popModal.js';// 实例化组件类
const modalConstructor = Vue.extend(setAccountModal);// 创建新Vue构造器
const options = {
            title: '新建配置项',
            tableVm: this.$refs.tableVm,
            clusterOptions: this.clusterOptions
        };
popModal(modalConstructor, options);// 实例化新Vue构造器,并传入参数
Vue 3 使用 TypeScript 有多种写法,以下是不同场景下的示例: ### 创建项目 使用以下命令创建一个支持 TypeScript 的 Vue 3 项目: ```bash npm create vue@latest ``` 在具体配置中,选择添加 TypeScript 支持,其他配置可按需选择。例如: ```plaintext √ Project name: vue3_test √ Add TypeScript? Yes √ Add JSX Support? No √ Add Vue Router for Single Page Application development? No √ Add Pinia for state management? No √ Add Vitest for Unit Testing? No √ Add an End-to-End Testing Solution? » No √ Add ESLint for code quality? Yes √ Add Prettier for code formatting? No ``` 以上操作步骤可参考官方文档 [^3]。 ### 为 props 声明默认值 在 vue3.4 及以下版本使用 `withDefaults` 为 props 声明默认值。`withDefaults` 帮助程序为默认值提供型检查,并确保返回的 props 型删除了已声明默认值的属性的可选标志。示例代码如下: ```vue <script setup lang="ts"> import { withDefaults, defineProps, defineEmits, onMounted } from 'vue' interface Props { msg?: string labels?: string[] } interface Emits { (e: 'change', value: string): void } const props = withDefaults(defineProps<Props>(), { msg: 'hello', labels: () => ['one', 'two'] }) const emits = defineEmits<Emits>() onMounted(() => { emits('change', 'hello world') }) </script> ``` [^2] ### 引入 Vue 自带的 Ref 型 可以选择引入 Vue 自带的 `Ref` 型,这并不会影响其他地方对该值的接收。示例如下: ```vue <script lang="ts"> import type { Ref } from 'vue'; import { ref, defineComponent } from 'vue'; interface Still { // 这里可根据实际情况定义 Still 型 } interface Finished { // 这里可根据实际情况定义 Finished 型 } interface TaskInfo { id: number; title: string; isCompleted: boolean; } export default defineComponent({ setup() { let still: Ref<Still> = ref(3); let finished: Ref<Finished> = ref(0); let todos: Ref<TaskInfo[]> = ref([ { id: 0, title: "task0", isCompleted: false }, { id: 1, title: "task1", isCompleted: true }, ]); return { still, finished, todos }; } }); </script> ``` [^4] ### reactive() 隐式推导型 `reactive()` 会隐式地从它的参数中推导型。示例代码如下: ```vue <script setup lang="ts"> import { reactive } from 'vue' // 推导得到的型:{ title: string } const book = reactive({ title: 'Vue 3 指引' }) </script> ``` [^5]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值