Html 引入element UI + vue3 报错Failed to resolve component: el-button

问题:Html 引入element UI + vue3 ,el-button效果不出来

复制代码
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- import Vue before Element -->
<!--  <script src="https://unpkg.com/vue@2/dist/vue.js"></script>-->
  <script src="js/vue3.3.8/vue.global.js"></script>
  <!-- import JavaScript -->
  <!--  <script src="js/elementUI/index.js"></script>-->
  <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
  <!-- import CSS -->
  <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">



</head>
<body>
<div id="app">
  <!-- 双大括号语法,可以直接拿到下面data中return 里面的数据 -->
  {{message}}
  <el-button type="success">成功按钮</el-button>
  <el-progress type="circle" :percentage="20"></el-progress>
</div>
</body>

<script>
 const { createApp, ref } = Vue
   createApp({
     setup() {
       const message = ref('Hello vue!')
       return {
         message
       }
     }
   }).mount('#app')

</script>
</html>

运行报错:

index.min.js:1 Uncaught TypeError: Cannot read properties of undefined (reading '$isServer')

Vue warn\]: Failed to resolve component: el-button If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement. at \

问题分析:

使用vue2的话运行正常,应该是vue3与element UI不兼容。

Element UI是一款基于Vue2.x 的界面框架;Element Plus是一款基于Vue3.x 的界面框架;

解决

vue3的话,改用element Plus

源码:

注意:在挂载vue之前,要加载elementplus

app.use(ElementPlus)

复制代码
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1.0" />
  <script src="https://unpkg.com/vue@next"></script>
  <!-- import CSS -->
   <link rel="stylesheet" href="https://unpkg.com/element-plus/dist/index.css">
  <!-- import JavaScript -->
  <script src="https://unpkg.com/element-plus"></script>

  <title>Element Plus demo</title>
</head>
<body>
<div id="app">
  <el-button type="primary">{{ message }}</el-button>
  <el-progress :percentage="20" type="circle"></el-progress>
</div>
<script>

    const {createApp, ref} = Vue
  const app = createApp({
    setup() {
      const message = ref('Hello vue!')
      return {
        message
      }
    }
  })
  app.use(ElementPlus)
  app.mount('#app')

</script>
</body>
</html>

指定特定版:

<script src=''></script>

<link rel="stylesheet" href="https://unpkg.com/browse/element-plus@2.4.2/dist/index.css">

<script src='https://unpkg.com/element-plus@2.4.2/dist/index.full.js'></script>

效果:
相关推荐
一枚前端小能手16 小时前
🔥 重学Vue之computed、watch、watchEffect原理与用途详解
前端·javascript·vue.js
Amos_Web16 小时前
Rust实战课程--网络资源监控器(初版)
前端·后端·rust
神秘的猪头16 小时前
html5与js今日笔记
前端
Zyx200716 小时前
JavaScript 中的对象字面量与代理模式:用代码演绎“爱情故事”
javascript
程序猿小蒜16 小时前
基于springboot的基于智能推荐的卫生健康系统开发与设计
java·javascript·spring boot·后端·spring
渣哥16 小时前
IOC 容器的进化:ApplicationContext 在 Spring 中的核心地位
javascript·后端·面试
Zyx200716 小时前
🎹用 HTML5 打造“敲击乐”钢琴:前端三剑客的第一次交响曲
前端
小时前端16 小时前
面试官:我为什么总在浏览器存储问题上追问IndexedDB?
前端·浏览器
前端小菜哇16 小时前
前端如何优雅的写一个记忆化函数?
前端
今禾16 小时前
Git完全指南(下篇):Git高级技巧与问题解决
前端·git·github