【心累了】Cannot read property 'use' of undefined

发布于 2022-09-12 22:46:40 字数 4587 浏览 30 评论 0

心累啊,vue-cli4项目使用ant-design-vue,引入FormModel为什么老是报错呢!!!不过就是想要个按需加载的功能咋怎么难呢,就这东西折腾了一天
image.png

image.png

ant.js

import {
  FormModel,
} from "ant-design-vue";
const ant = {
  install(Vue) {
    Vue.component(FormModel.name, FormModel);
  },
};
export default ant;

main.js

import {
  createApp
} from 'vue'

/* 引入ant-design */
// import Antd from 'ant-design-vue';
// 按需加载
// import {
//   FormModel
// } from 'ant-design-vue';
import ant from "./utils/ant";
// import 'ant-design-vue/dist/antd.less';
/* 引入ant-design */

import Login from './views/Login.vue'
import router from './router'
import store from './store'

const app = createApp(Login)
// 开发者工具
app.config.devtools = true;
app.use(store).use(router).use(ant).mount('#app')

Login.vue

<template>
  <a-form-model ref="ruleForm" :model="ruleForm" :rules="rules" v-bind="layout">
    <a-form-model-item has-feedback label="User" prop="user">
      <a-input v-model.number="ruleForm.user" />
    </a-form-model-item>
    <a-form-model-item has-feedback label="Password" prop="pass">
      <a-input v-model="ruleForm.pass" type="password" autocomplete="off" />
    </a-form-model-item>
    <a-form-model-item has-feedback label="Confirm" prop="checkPass">
      <a-input
        v-model="ruleForm.checkPass"
        type="password"
        autocomplete="off"
      />
    </a-form-model-item>

    <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
      <a-button type="primary" @click="submitForm('ruleForm')">
        Submit
      </a-button>
      <a-button style="margin-left: 10px" @click="resetForm('ruleForm')">
        Reset
      </a-button>
    </a-form-model-item>
  </a-form-model>
</template>
<script>
export default {
  name: "login",
  data() {
    let checkPending;
    let checkAge = (rule, value, callback) => {
      clearTimeout(checkPending);
      if (!value) {
        return callback(new Error("Please input the age"));
      }
      checkPending = setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("Please input digits"));
        } else {
          if (value < 18) {
            callback(new Error("Age must be greater than 18"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("Please input the password"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("Please input the password again"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("Two inputs don't match!"));
      } else {
        callback();
      }
    };
    let validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("Two inputs don't match!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        user: "",
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: "change", required: "true" },
        ],
        checkPass: [
          { validator: validatePass2, trigger: "change", required: "true" },
        ],
        user: [{ validator: checkAge, trigger: "change", required: "true" }],
      },
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 },
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {},
};
</script>
<style lang="less">
@w_800: 800px;
@w_400: 450px;
.mar_pad {
  margin: 0 auto;
  padding: 0;
}
.wh_percent {
  width: 100%;
  height: 100%;
}

.container {
  .wh_percent;
  .form_content {
    width: @w_400;
  }
}
</style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与昨日 2022-09-19 22:46:40

你好,我完成了低版本情况下的按需引入,可以参考这篇文章

引用https://www.cnblogs.com/lepan...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文