vue-cli4.0引入官方文档Layout示例过程(按需引入)

写在前面:建议在了解了如何按需引入一个简单的按钮后再看本篇。

常见报错

  1. [Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the “name” option.
  2. [Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the “name” option.
  3. [Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the “name” option.
  4. [Vue warn]: Error in render: “TypeError: Cannot read property ‘propsData’ of undefined”
  5. TypeError: Cannot read property ‘propsData’ of undefined
  6. …and so on…就不一一列举了

踩完坑后可以确定的是,上面的错误都是因为组件没有正确引入、注册引起的。

直接上代码(包括局部注册全局注册两种方式),引入antd官方文档的一个例子,如下图
在这里插入图片描述

1.局部注册

我是在App.vue里直接改的。

  1. 先将官方示例代码<a-layout> ... </a-layout>部分copy到我们的<div id="app"></div>
  2. 然后再把css样式复制到末尾。这时候你可以看一下浏览器控制台,会有一堆报错,告诉你好多东西没有引入/注册
  3. 因此,最后要做是引入组件、注册组件
<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>
                <a-menu-item key="3">
                    option3
                </a-menu-item>
                <a-menu-item key="4">
                    option4
                </a-menu-item>
                </a-sub-menu>
                <a-sub-menu key="sub2">
                <span slot="title"><a-icon type="laptop" />subnav 2</span>
                <a-menu-item key="5">
                    option5
                </a-menu-item>
                <a-menu-item key="6">
                    option6
                </a-menu-item>
                <a-menu-item key="7">
                    option7
                </a-menu-item>
                <a-menu-item key="8">
                    option8
                </a-menu-item>
                </a-sub-menu>
                <a-sub-menu key="sub3">
                <span slot="title"><a-icon type="notification" />subnav 3</span>
                <a-menu-item key="9">
                    option9
                </a-menu-item>
                <a-menu-item key="10">
                    option10
                </a-menu-item>
                <a-menu-item key="11">
                    option11
                </a-menu-item>
                <a-menu-item key="12">
                    option12
                </a-menu-item>
                </a-sub-menu>
            </a-menu>
            </a-layout-sider>
            <a-layout-content :style="{ padding: '0 24px', minHeight: '280px' }">
            Content
            </a-layout-content>
        </a-layout>
        </a-layout-content>
        <a-layout-footer style="text-align: center">
        Ant Design ©2018 Created by Ant UED
        </a-layout-footer>
    </a-layout>
  </div>
</template>

<script>

import { Layout, Icon, Menu, Breadcrumb } from 'ant-design-vue' // 引入 

export default {
  name: 'App',
  data() {
    return {
      collapsed: false,
    };
  },
  components:{          // 注册 
    AIcon: Icon,
    
    ALayout: Layout,
    ALayoutHeader: Layout.Header,   //子组件也要注册,这个地方对新手来说太坑了
    ALayoutSider: Layout.Sider,
    ALayoutContent: Layout.Content,
    ALayoutFooter: Layout.Footer,

    AMenu: Menu,
    ASubMenu: Menu.SubMenu,
    AMenuItem: Menu.Item,
    
    ABreadcrumb: Breadcrumb,
    ABreadcrumbItem: Breadcrumb.Item,
  }
}
</script>

<style>
#components-layout-demo-top-side .logo {
  width: 120px;
  height: 31px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px 28px 16px 0;
  float: left;
}
</style>

2.全局注册

main.js

import Vue from 'vue'
import App from './App.vue'

import { Layout, Icon, Menu, Breadcrumb } from 'ant-design-vue' // 引入 

Vue.component(Icon.name, Icon)        // 注册

Vue.component(Layout.name, Layout);
Vue.component(Layout.Header.name, Layout.Header);
Vue.component(Layout.Content.name, Layout.Content);
Vue.component(Layout.Footer.name, Layout.Footer);
Vue.component(Layout.Sider.name, Layout.Sider);

Vue.component(Menu.name, Menu);
Vue.component(Menu.SubMenu.name, Menu.SubMenu);
Vue.component(Menu.Item.name, Menu.Item);

Vue.component(Breadcrumb.name, Breadcrumb);
Vue.component(Breadcrumb.Item.name, Breadcrumb.Item)

Vue.config.productionTip = false;

new Vue({
  render: h => h(App),
}).$mount('#app')

App.vue

<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>
                <a-menu-item key="3">
                    option3
                </a-menu-item>
                <a-menu-item key="4">
                    option4
                </a-menu-item>
                </a-sub-menu>
                <a-sub-menu key="sub2">
                <span slot="title"><a-icon type="laptop" />subnav 2</span>
                <a-menu-item key="5">
                    option5
                </a-menu-item>
                <a-menu-item key="6">
                    option6
                </a-menu-item>
                <a-menu-item key="7">
                    option7
                </a-menu-item>
                <a-menu-item key="8">
                    option8
                </a-menu-item>
                </a-sub-menu>
                <a-sub-menu key="sub3">
                <span slot="title"><a-icon type="notification" />subnav 3</span>
                <a-menu-item key="9">
                    option9
                </a-menu-item>
                <a-menu-item key="10">
                    option10
                </a-menu-item>
                <a-menu-item key="11">
                    option11
                </a-menu-item>
                <a-menu-item key="12">
                    option12
                </a-menu-item>
                </a-sub-menu>
            </a-menu>
            </a-layout-sider>
            <a-layout-content :style="{ padding: '0 24px', minHeight: '280px' }">
            Content
            </a-layout-content>
        </a-layout>
        </a-layout-content>
        <a-layout-footer style="text-align: center">
        Ant Design ©2018 Created by Ant UED
        </a-layout-footer>
    </a-layout>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      collapsed: false,
    };
  },
}
</script>

<style>
#components-layout-demo-top-side .logo {
  width: 120px;
  height: 31px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px 28px 16px 0;
  float: left;
}
</style>

结果如下,一切正常
在这里插入图片描述
有点困了,遭不住。先这样吧。本片略过了常规的步骤,仅列出了一些要点。如果有什么疑惑,欢迎评论区提出

  • 2
    点赞
  • 3
    收藏
  • 打赏
    打赏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论

打赏作者

LitStronger

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值