Vue开发实例(三)项目引入Element-UI

项目引入Element-UI

一、引入Element-UI

vue 复制代码
npm i element-ui --save
npm install element-ui -S

等待安装完成

二、注册组件

1、vue2使用element-ui

在main.js中注册

vue 复制代码
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// Vue3不是这种语法,请注意
Vue.use(ElementUI);

vue2全部main.js代码

vue 复制代码
import Vue from 'vue'
import App from './App.vue'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.config.productionTip = false
Vue.use(ElementUI);

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

注意:在使用Vue3全局注册element的时候,发现错误如下:

Uncaught TypeError: Cannot read properties of undefined (reading 'prototype')

at eval (webpack-internal:///./node_modules/element-ui/lib/utils/types.js:39)

at Object.../node_modules/element-ui/lib/utils/types.js (chunk-vendors.js:2923)

at webpack_require (app.js:849)

at fn (app.js:151)

at eval (webpack-internal:///./node_modules/element-ui/lib/utils/util.js:19)

at Object.../node_modules/element-ui/lib/utils/util.js (chunk-vendors.js:2935)

at webpack_require (app.js:849)

at fn (app.js:151)

at eval (webpack-internal:///./node_modules/element-ui/lib/locale/format.js:49)

at Object.../node_modules/element-ui/lib/locale/format.js (chunk-vendors.js:2526)

那么如何解决呢,继续往下看...

2、vue3使用element-ui

vue3中使用element-plus,它对Vue3进行了支持。

vue 复制代码
npm install element-plus --save

vue3 在main.js注册element-plus

注意这里与Vue2的注册有些不一样,比如:

vue 复制代码
const app = createApp(App);

先创建app 在用 use

vue 复制代码
import { createApp } from 'vue'
import App from './App.vue'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
const app = createApp(App);
app.use(ElementPlus);

app.mount('#app')

三、使用Element组件

1、轻微改造

启动项目,正常显示即代表项目运行正常

改造项目代码

位置:HelloWorld.vue

vue 复制代码
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
}
</script>

<style scoped>
</style>

这样代码看上去比较简单,干净

2、验证element是否生效

vue 复制代码
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <el-button>这是一个按钮</el-button>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
}
</script>

<style scoped>
</style>

从上面的页面可以看到,element注册成功了

相关推荐
JustHappy6 小时前
古法编程秘籍(七):互联网到底是什么?把两台电脑怎么说话搞懂就够了
前端·后端·网络协议
snow@li6 小时前
SEO-文章标题:写文章时候,分类+主标题+大纲+解释 作为标题 / 不点进去也知道全文覆盖什么 / 标题即架构
前端
kyriewen7 小时前
Git Commit 前自动修复代码风格?配置 Husky + lint-staged,从此 CR 只聊逻辑
前端·git·面试
岁月宁静7 小时前
RAG 文档摄入全链路,从原理到生产落地
vue.js·人工智能·python
小和尚同志7 小时前
AI 自动化测试探索(一):Playwright MCP
前端·人工智能·aigc
老马识途2.07 小时前
在AI的帮助下理解spring的启动过程
java·前端·spring
徐小夕8 小时前
Loop Engineering 深度解析与实战指南(全网最全)
前端·算法·github
运筹vivo@8 小时前
Python ContextVar 底层机制与内存模型拆解
前端·数据库·python
默子昂9 小时前
ollama 自定义ui
开发语言·python·ui
#麻辣小龙虾#9 小时前
基于vue3.0开发一款【固废与废气运维管理系统】(支持源码)
前端·vue.js·vue3