vant icon indicating copy to clipboard operation
vant copied to clipboard

nuxt.js中无法使用按需加载

Open xlkit456852 opened this issue 7 years ago • 2 comments

IMPORTANT: Please use the following link to create a new issue. If your issue was not created using the app above, it will be closed immediately.

注意:请按照下面的模板来新建 issue,不规范的 issue 会被立即关闭.

Describe the bug 在nuxt中配置vant按需使用后,npm run dev成功,但运行时报错,无法按需加载

Screenshots _20181230161434

Environment

  • Device: PC浏览器
  • Browser: Chrome
  • Vant Version: 1.5.0

Reproduce package.json

"dependencies": {
    "@nuxtjs/axios": "^5.0.0",
    "cross-env": "^5.2.0",
    "express": "^4.16.3",
    "nuxt": "^2.0.0",
    "vant": "^1.5.0"
  },
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-plugin-import": "^1.11.0",
    "babel-preset-env": "^1.7.0",
    "less": "^3.9.0",
    "less-loader": "^4.1.0",
    "nodemon": "^1.11.0",
    "vux-loader": "^1.2.9"
  }

nuxt.config.js

build: {
        /*
         ** You can extend webpack config here
         */
        extend(config, { isDev, isClient }) {

        },
        babel:{        //配置按需引入规则
            "plugins": [
                ["import", {
                    "libraryName": "vant",
                    "libraryDirectory": "es",
                    "style": true
                }]
            ]
        },
    }

组件中使用: banner-block.vue:

<template>
<div class="swiper-block">
            <van-swipe >
                <van-swipe-item v-for="(item,index) in banner_data" v-key="index">
                    <img :src="item.image"/>
                </van-swipe-item>
            </van-swipe>
        </div>
</template>

<script>
    import { Swipe, SwipeItem } from 'vant';
    export default {
        components: {
            Swipe,
            SwipeItem
        },
        ......
    }
</script>

xlkit456852 avatar Dec 30 '18 08:12 xlkit456852

babel-plugin-import 目前不支持在 nuxt 中自动引入样式,建议把 style: true 去掉试试

chenjiahan avatar Jan 08 '19 14:01 chenjiahan

配置这样写: ` build: { // 添加这个是关键,添加后babel才会处理依赖包vant里面的代码 transpile: [ /vant.*?less/ ],

babel: { plugins: [ ['import', { libraryName: 'vant', "style": (name) => { return ${name}/style/less.js } }, 'vant'] ], }, }` 具体参考我的文章: https://juejin.im/post/5e012778518825127f085da8

gp0320 avatar Dec 26 '19 03:12 gp0320