【心累了】Cannot read property 'use' of undefined
心累啊,vue-cli4项目使用ant-design-vue,引入FormModel为什么老是报错呢!!!不过就是想要个按需加载的功能咋怎么难呢,就这东西折腾了一天
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你好,我完成了低版本情况下的按需引入,可以参考这篇文章