Ant Design Vue引入Form组件样式不显示的解决方法

使用 vue-cli 安装 Ant Design Vue 的 Form 组件,发现不能用,组件样式不显示。

Ant Design Vue引入Form组件样式不显示的解决方法 HTML笔记 第1张

此处省略了安装和初始化的步骤,需要的点击上方链接,直接在 Ant Design 官网查看教程

在 main.js 中全局引入:

import { Form, Input, Select } from 'ant-design-vue';

因为我进行了 按需加载组件代码 的配置,所以只需要上面一行就可以。

引用官网的一个基础示例:

<template>
  <a-form :form="form" @submit="handleSubmit">
    <a-form-item label="Note" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
      <a-input
        v-decorator="['note', { rules: [{ required: true, message: 'Please input your note!' }] }]"
      />
    </a-form-item>
    <a-form-item label="Gender" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
      <a-select
        v-decorator="[
          'gender',
          { rules: [{ required: true, message: 'Please select your gender!' }] },
        ]"
        placeholder="Select a option and change input text above"
        @change="handleSelectChange"
      >
        <a-select-option value="male">
          male        </a-select-option>
        <a-select-option value="female">
          female        </a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
      <a-button type="primary" html-type="submit">
        Submit      </a-button>
    </a-form-item>
  </a-form></template><script>export default {
  data() {    return {      formLayout: 'horizontal',      form: this.$form.createForm(this, { name: 'coordinated' }),
    };
  },  methods: {
    handleSubmit(e) {
      e.preventDefault();      this.form.validateFields((err, values) => {        if (!err) {          console.log('Received values of form: ', values);
        }
      });
    },
    handleSelectChange(value) {      console.log(value);      this.form.setFieldsValue({        note: `Hi, ${value === 'male' ? 'man' : 'lady'}!`,
      });
    },
  },
};</script>

发现,没有样式,只显示文字

Ant Design Vue引入Form组件样式不显示的解决方法 HTML笔记 第2张

因为只引入是不行的,还需要 注册组件 才能正常使用。

需要用到哪些组件,都要进行注册:

Vue.component(Form.name, Form);
Vue.component(Form.Item.name, Form.Item);
Vue.component(Input.name, Input);
Vue.component(Select.name, Select);
Vue.component(Select.Option.name, Select.Option);

这样组件就可以正常显示了。

推荐阅读:

Z-Blog后台无法安装、更新应用的解决方法

https百度推送push.js报错的解决方法

给网站添加熊掌号粉丝关注按钮

判断网页是通过PC端还是移动终端打开的

各大网站CSS初始化代码集合

赞 (1)
分享到: +

评论 沙发

Avatar

换个身份

  • 昵称 (必填)
  • 邮箱 (选填)