头图
Using vue3 to introduce Element plus, when using icon, it is found that it is not properly rendered on the page. After checking the official website, I found that if you want to use it directly like a use case, you need to register the component globally before you can use it directly in the project .

single icon introduction

You can import in a single component like this:

 <template>
  <el-menu
      active-text-color="#ffd04b"
      background-color="#545c64"
      class="el-menu"
      default-active="2"
      text-color="#fff"
      @open="handleOpen"
      @close="handleClose"
  >
    <el-sub-menu index="1">
      <template #title>
        <el-icon><location /></el-icon>
        <span>Navigator One</span>
      </template>
    </el-sub-menu>
  </el-menu>
</template>

<script>
import { Location } from '@element-plus/icons-vue'

export default {
  name: "SideMenu",
  components: {
    Location
  }
}
</script>

Register globally as a component

But doing this every time is obviously too cumbersome, we can use the following method to globally register the icon as a component:

 // main.js
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElIcons from '@element-plus/icons-vue'

import App from './App.vue'

const app = createApp(App)

// 统一注册Icon图标
for (const iconName in ElIcons) {
    app.component(iconName, ElIcons[iconName])
}

app.mount('#app')

This is much more convenient to use.


来了老弟
508 声望31 粉丝

纸上得来终觉浅,绝知此事要躬行