vue3.2+TS

3.2代码如下(示例):

<template>
  <div>
    <el-card>
      <el-form
        ref="form"
        label-width="110px"
        size="mini"
        class="search-form"
        :model="state.search"
        :rules="state.rules"
        :inline="true"
      >
        <!-- 航班号 -->
        <el-form-item
          label="航班号:"
          prop="flightNo"
        >
          <el-input
            v-model="state.search.flightNo"
            placeholder="请输入航班"
            maxlength="10"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            type="primary"
            @click="submit"
          >提交{{count}}</el-button>
          <el-button
            size="mini"
            type="warning"
            @click="onReset"
          >重置{{num}}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            type="primary"
            @click="showDialog = !showDialog"
          >{{!showDialog?'打开':'关闭'}}模态框</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            type="primary"
            @click="to"
          >跳转路由</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
  <!-- 传送门 -->
  <teleport to="body">
    <div
      class="modal"
      v-if="showDialog"
      style="position: fixed"
    >
      fw
      <el-button
        size="mini"
        type="primary"
        @click="showDialog = false"
      >关闭</el-button>
    </div>
  </teleport>
</template>
<script  lang="ts" setup>
import { reactive, ref, onMounted, computed, watch } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { ElNotification } from "element-plus";
import { key } from "../store";
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
});
interface ProjectList {
  id: string;
  name: string;
}
//类型
type Search = {
  sourceType: number;
  arr: string;
  dep: string;
  flightNo: string | null;
  flightDate: any;
  ticketDate: any;
  ticketNo: string | null;
};
//路由
const router = useRouter();
//vuex
const store = useStore(key);
/**
 * 初始化对象
 */
const my_search: Search = {
  sourceType: 1,
  arr: "",
  dep: "",
  flightNo: "",
  flightDate: [],
  ticketDate: [],
  ticketNo: null,
};
const state = reactive({
  data: [] as ProjectList[],
  datalist: <any>[],
  search: my_search as Search,
  showDialog: false,
  //表单校验
  rules: {
    flightNo: [{ required: true, message: "请输入航班", trigger: "blur" }],
  },
});
watch(
  () => state.search.flightNo,
  () => {
    console.log("航班号改变");
  }
);
const { search } = state;
const form = ref(null);
//计算属性
const number = computed({
  get: () => search.sourceType + 10,
  set: (val) => {
    search.sourceType = val;
  },
});
//计算属性
const num = computed({
  get: () => search.sourceType + 10,
  set: (val) => {
    search.sourceType = val;
  },
});
/**
 * 提交
 */
const submit = async (): Promise<void> => {
  const ref: any = form.value;
  ref.validate((valid: any) => {
    if (valid) {
      ElNotification({
        title: "成功",
        message: "恭喜你,这是一条成功消息",
        type: "success",
      });
    }
  });
};
/**
 * 重置
 */
const onReset = (): void => {
  state.showDialog = false;
  state.search = my_search;
  state.data = [];
  //操作dom
  const ref: any = form.value;
  search.sourceType++;
  store.commit("increment", search.sourceType);
  ref.resetFields();
};
/**
 * 路由跳转
 */

const to = () => {
  router.push({
    path: "/helloWorld",
    query: { name: 222 },
  });
};
const count = computed(() => store.state.count);
onMounted(() => {
  console.log("onMounted=>", props.visible);
});
</script>

<style lang="scss" scoped>
</style>

3.0代码如下(示例):

<template>
  <div>
    <el-card>
      <el-form
        ref="form"
        label-width="110px"
        size="mini"
        class="search-form"
        :model="search"
        :rules="rules"
        :inline="true"
      >
        <!-- 航班号 -->
        <el-form-item
          label="航班号:"
          prop="flightNo"
        >
          <el-input
            v-model="search.flightNo"
            placeholder="请输入航班"
            maxlength="10"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            type="primary"
            @click="submit"
          >提交{{count}}</el-button>
          <el-button
            size="mini"
            type="warning"
            @click="onReset"
          >重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            type="primary"
            @click="showDialog = !showDialog"
          >{{!showDialog?'打开':'关闭'}}模态框</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            type="primary"
            @click="to"
          >跳转路由</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
  <!-- 传送门 -->
  <teleport to="body">
    <div
      class="modal"
      v-if="showDialog"
      style="position: fixed"
    >
      fw
      <el-button
        size="mini"
        type="primary"
        @click="showDialog = false"
      >关闭</el-button>
    </div>
  </teleport>
</template>
<script lang="ts">
import {
  defineComponent,
  reactive,
  ref,
  onMounted,
  toRefs,
  computed,
} from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { ElMessage, ElNotification } from "element-plus";
import { key } from "../store";
//接口
interface ProjectList {
  id: string;
  name: string;
}
//类型
type Search = {
  sourceType: number;
  arr: string;
  dep: string;
  flightNo: string | null;
  flightDate: any;
  ticketDate: any;
  ticketNo: string | null;
};

export default defineComponent({
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    //监听数组
    data: {
      handler(val, oldVal) {
        console.log("数组改变");
      },
      deep: true,
    },
    props: {
      handler(val, oldVal) {
        this.state.showDialog = val.visible;
      },
    },
    "search.flightNo": {
      handler(val, oldVal) {
        console.log("航班号改变");
      },
    },
  },

  setup(props) {
    //路由
    const router = useRouter();
    //vuex
    const store = useStore(key);
    /**
     * 初始化对象
     */
    const my_search: Search = {
      sourceType: 1,
      arr: "",
      dep: "",
      flightNo: "",
      flightDate: [],
      ticketDate: [],
      ticketNo: null,
    };
    const state = reactive({
      data: [] as ProjectList[],
      datalist: <any>[],
      search: my_search as Search,
      showDialog: false,
      //表单校验
      rules: {
        flightNo: [{ required: true, message: "请输入航班", trigger: "blur" }],
      },
    });
    const { search } = state;
    const form = ref(null);
    //计算属性
    const number = computed({
      get: () => search.sourceType + 10,
      set: (val) => {
        search.sourceType = val;
      },
    });
    //计算属性
    const num = computed({
      get: () => search.sourceType + 10,
      set: (val) => {
        search.sourceType = val;
      },
    });
    const methods = {
      /**
       * 提交
       */
      submit: async (): Promise<void> => {
        const ref: any = form.value;
        ref.validate((valid: any) => {
          if (valid) {
            // ElMessage({
            //   message: "恭喜你,这是一条成功消息",
            //   type: "success",
            // });
            ElNotification({
              title: "成功",
              message: "恭喜你,这是一条成功消息",
              type: "success",
            });
            search.sourceType++;

            store.commit("increment");
          }
        });
      },
      /**
       * 重置
       */
      onReset: (): void => {
        state.showDialog = false;
        state.search = my_search;
        state.data = [];
        //操作dom
        const ref: any = form.value;
        ref.resetFields();
      },
      /**
       * 路由跳转
       */

      to: () => {
        router.push({
          path: "/helloWorld",
          query: { name: 222 },
        });
      },
    };
    const count = computed(() => store.state.count);
    onMounted(() => {
      console.log("onMounted=>", props.visible);
    });
    return { ...methods, ...toRefs(state), form, number, num, count };
  },
});
</script>

<style lang="scss" scoped>
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值