写在前面:建议在了解了如何按需引入一个简单的按钮后再看本篇。
常见报错
- [Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the “name” option.
- [Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the “name” option.
- [Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the “name” option.
- [Vue warn]: Error in render: “TypeError: Cannot read property ‘propsData’ of undefined”
- TypeError: Cannot read property ‘propsData’ of undefined
- …and so on…就不一一列举了
踩完坑后可以确定的是,上面的错误都是因为组件没有正确引入、注册引起的。
直接上代码(包括局部注册和全局注册两种方式),引入antd官方文档的一个例子,如下图
1.局部注册
我是在App.vue里直接改的。
- 先将官方示例代码
<a-layout> ... </a-layout>
部分copy到我们的<div id="app"></div>
内 - 然后再把css样式复制到末尾。这时候你可以看一下浏览器控制台,会有一堆报错,告诉你好多东西没有引入/注册
- 因此,最后要做是引入组件、注册组件
<template>
<div id="app">
<a-layout id="components-layout-demo-top-side">
<a-layout-header class="header">
<div class="logo" />
<a-menu
theme="dark"
mode="horizontal"
:default-selected-keys="['2']"
:style="{ lineHeight: '64px' }"
>
<a-menu-item key="1">
nav 1
</a-menu-item>
<a-menu-item key="2">
nav 2
</a-menu-item>
<a-menu-item key="3">
nav 3
</a-menu-item>
</a-menu>
</a-layout-header>
<a-layout-content style="padding: 0 50px">
<a-breadcrumb style="margin: 16px 0">
<a-breadcrumb-item>Home</a-breadcrumb-item>
<a-breadcrumb-item>List</a-breadcrumb-item>
<a-breadcrumb-item>App</a-breadcrumb-item>
</a-breadcrumb>
<a-layout style="padding: 24px 0; background: #fff">
<a-layout-sider width="200" style="background: #fff">
<a-menu
mode="inline"
:default-selected-keys="['1']"
:default-open-keys="['sub1']"
style="height: 100%"
>
<a-sub-menu key="sub1">
<span slot="title"><a-icon type="user" />subnav 1</span>
<a-menu-item key="1">
option1
</a-menu-item>
<a-menu-item key="2">
option2
</a-menu-item