vue使用babel-plugin-import按需引入Ant Design View + babel-plugin-component按需引入element-ui


0.基本步驟

 

官網直通車——在 vue-cli 中使用 - Ant Design Vue (antdv.com)

1. Ant Design View按需引入

我的低版本的古老vue-cli模板里修改如下

 

 

 

 main.js里修改如下

 

 效果如圖

 2. Element-UI按需引入(低版本)

官網直通車——組件 | Element

先說說和上面的ant design不同的地方

 

 

 3、Element-UI(vue-cli4高版本)按需引入

昨日一直遇到

export ‘default‘ (imported as ‘Vue‘) was not found in ‘vue】和【Cannot read property 'use' of undefined

這兩個問題,當時以為是自己按需引入的問題,現在發現應該是我以前用vue-cli低版本

vue init webpack [projectName]

生成的項目和vue-cli高版本

vue create [projectName]

兩者生成模板不同,我直接把配置c+v過來,造成的不知何處的版本遷移問題,就導致我這個按需引入一直失敗,相當自閉。

今天過來重新生成項目,純凈環境(之前cv過來很多其他的東西,哎心累)下再次進行

vue add element

這個可以自己選擇按需引入,不用手寫前面的babel.config.js(低版本是.babelrc),個人覺得挺方便的。

自動生成的babel.config.js內容如下

module.exports = {
  "presets": [
    "@vue/cli-plugin-babel/preset"
  ],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

在Main.js文件里,掛上按需引入的組件(這里如果放在自動生成的plugins/element.js樣式還是不出來,不知道為什么)

import Vue from 'vue'
import App from './App.vue'
// import './plugins/element.js'
import { Button } from 'element-ui'

Vue.use(Button)
Vue.config.productionTip = false

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

PS.plugins/element.js文件內容如下

import Vue from 'vue'
import { Button } from 'element-ui'

Vue.use(Button)

在入口App.vue文件里

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <div>
      <p>
        If Element is successfully added to this project, you'll see an
        <code v-text="'<el-button>'"></code>
        below
      </p>
      <el-button @click="handleClick">el-button</el-button>
    </div>
    <!-- <HelloWorld msg="Welcome to Your Vue.js App"/> -->
  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'app',
  // components: {
  //   HelloWorld
  // }
    methods: {
    handleClick() {
      console.log("我還能看見以往的顏色嗎?");
    },
  },
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

效果如圖

 

 PPS:這是【冰海戰記】OP【MUKANJYO】里的歌詞改寫,昨日相當痛苦的時候一直在聽這首歌,心情舒緩了不少。

MUKANJYO(TV動畫《冰海戰記》片頭曲) - Survive Said The Prophet - 單曲 - 網易雲音樂 (163.com)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM