一、初始化 vue3 项目

在终端中进入存放项目路径文件夹,然后使用命令:

vue create project

这时候 vue 会生成项目的基础内容。

二、安装 element-plus

在项目路径底下使用以下命令进行安装:

npm install element-plus —save

三、引入 element-plus

3.1 main.js

  1. import { createApp } from 'vue'
  2. import ElementPlus from 'element-plus'
  3. import 'element-plus/dist/index.css'
  4. import App from './App.vue'
  5. const app = createApp(App)
  6. app.use(ElementPlus)
  7. app.mount('#app')

3.2 引入 Element 组件

3.2.1 在 component 目录下新建 MyElButton.vue 组件

  1. <template>
  2. <div>
  3. <el-row>
  4. <el-button @click="openMessage">打开通知</el-button>
  5. <el-button type="primary" >弹框</el-button>
  6. <el-button type="success" >打开对话框</el-button>
  7. <el-button type="info" >通知消息</el-button>
  8. <el-button type="warning">警告按钮</el-button>
  9. <el-button type="danger">危险按钮</el-button>
  10. </el-row>
  11. </div>
  12. </template>
  13. <script>
  14. import { ElMessage} from 'element-plus'
  15. export default {
  16. name: "MyElButton",
  17. setup(){
  18. /**
  19. * 打开通知按钮点击事件的响应
  20. */
  21. let openMessage =() =>{
  22. ElMessage({
  23. message: "成功",
  24. type: "success"
  25. })
  26. }
  27. return{
  28. openMessage
  29. }
  30. }
  31. }
  32. </script>
  33. <style scoped>
  34. #app {
  35. font-family: Avenir, Helvetica, Arial, sans-serif;
  36. -webkit-font-smoothing: antialiased;
  37. -moz-osx-font-smoothing: grayscale;
  38. text-align: center;
  39. color: #2c3e50;
  40. margin-top: 60px;
  41. }
  42. </style>

3.2.2 在 App.vue 中加载 MyElButton 组件

  1. <template>
  2. <div id = "app">
  3. <MyElButton>
  4. </MyElButton>
  5. </div>
  6. </template>
  7. <script>
  8. import MyElButton from './components/MyElButton.vue'
  9. export default {
  10. name: 'app',
  11. // 注册导入的组件
  12. components: {
  13. MyElButton
  14. }
  15. }
  16. </script>

四、运行结果

输入 npm run serve 运行项目,访问
image.png

点击「打开通知」按钮,显示成功。
image.png

五、参考资料