Skip to content

Using modularized antd with babel-plugin-import doesn't work on sub components like MenuItem, SubMenu, LayoutSlider / 按需加载无法在MenuItem、SubMenu、LayoutSlider等二级组件上应用 #3256

@mescab

Description

@mescab
  • I have searched the issues of this repository and believe that this is not a duplicate.

Version

2.0.0-rc.2

Environment

macOS 10.15.6, WebStrom 2020.2.3, Vue: 3.0.0, Antdv: 2.0.0-rc.1 or 2.0.0-rc.2

Reproduction link

https://github.com/vueComponent/ant-design-vue/releases/tag/2.0.0-rc.2

Steps to reproduce

  1. vue create testVue3
  2. npm install ant-design-vue@2.0.0-rc.2 (or ant-design-vue@2.0.0-rc.1)
  3. create a component TestComponent referred to by App.vue which import Menu and MenuItem
  4. enable babel-plugin-import in a way as below:
"plugins": [
["import", {"libraryName": "ant-design-vue", "libraryDirectory": "es", "style": "css"}]
]

and TestComponent:

<template>
    <Menu theme="dark"> 
        <MenuItem key="1">
            <span class="nav-text">Dashboard</span>
        </MenuItem>
        <MenuItem key="2">
            <span class="nav-text">Dashboard</span>
        </MenuItem>
    </Menu>
</template>

<script>

import MenuItem from "ant-design-vue/lib/menu/MenuItem"  // how to make this work??? this is the key
import {Menu} from 'ant-design-vue'

export default {
    name: "TestHomeLayout",
        components: {
            Menu,
            MenuItem
        },
}
</script>

The code above got no errors or warnings from "npm run serve", but finally result in a blank page in browser.

Open the browser inspection panel, the DOM tree look like:

<div id="app" data-v-app="">
    <div id="app">
        <ul role="menu" class="ant-menu-dark ant-menu-root ant-menu ant-menu-vertical" data-v-7e3d40e0="">
            <li role="menuitem" class="undefined-item" data-v-7e3d40e0="">
                <span class="nav-text" data-v-7e3d40e0="">Dashboard</span>
            </li>
            <li role="menuitem" class="undefined-item" data-v-7e3d40e0="">
                <span class="nav-text" data-v-7e3d40e0="">Settings</span>
            </li>
        </ul>
    </div>
</div>

The code above looks like the Menu component did work, but the MenuItem didn't. I don't know why and what's exactly the right way to import sub component.

I tried a few alternative ways and other sub components, but none of them walk me out.

Apart from that, if we use the '<a-menu>' and '<a-menu-item>' notation with a total import of ant-design-vue from main.js, everything is OK.

import { createApp } from 'vue'
import App from './App.vue'
import {router} from "@/router";

import Antd from 'ant-design-vue/dist/antd';   // one time complete import
import 'ant-design-vue/dist/antd.css';

import '@/global.less'

import store from '@/store'

const app = createApp(App)

app.use(router).use(store).use(Antd).mount('#app')

What is expected?

Using modularized antd should have same outcomes as one time complete import. / 按需加载和一次性全部加载在显示上应该没有区别。

What is actually happening?

Using modularized antd doesn't load sub component as expected.


Production build size is critical in my case, so we went to use modularized way to load antd component on demand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions