Vue3 搭配 Element-Plus

一、初始化 vue3 项目

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

vue create project

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

二、安装 Element-Plus

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

npm install element-plus --save

三、引入 element-plus

3.1 main.js

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'


const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')

3.2 引入 Element 组件

3.2.1 在 component 目录下新建 MyElButton.vue 组件
<template>
<div>
	<el-row>
		<el-button @click="openMessage">打开通知</el-button>
		<el-button type="primary" >弹框</el-button>
		<el-button type="success" >打开对话框</el-button>
		<el-button type="info" >通知消息</el-button>
		<el-button type="warning">警告按钮</el-button>
		<el-button type="danger">危险按钮</el-button>
	</el-row>
	</div>
</template>

<script>
	import { ElMessage} from 'element-plus'
	export default {
		name: "MyElButton",
		setup(){
			/**
			 * 打开通知按钮点击事件的响应
			 */
			let openMessage =() =>{
				ElMessage({
					message: "成功",
					type: "success"
				})
			}
			return{
				openMessage
			}
		}
	}
</script>

<style scoped>
	#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>
3.2.2 在 App.vue 中加载 MyElButton 组件
<template>
  <div id = "app">
    <MyElButton>
    </MyElButton>
  </div>
</template>

<script>
import MyElButton from './components/MyElButton.vue'
export default {
  name: 'app',
  // 注册导入的组件
  components: {
    MyElButton
  }
}
</script>

四、运行结果

输入 npm run serve 运行项目,访问。

点击「打开通知」按钮,弹出通知成功。

五、参考资料

Day.js是一个轻量级的时间处理库,非常适合在Vue应用中配合Element Plus使用,因为它提供了简单易用的API和与JavaScript Date对象良好的兼容性。要在Vue 3中结合Day.jsElement Plus,你可以按照以下步骤操作: 1. **安装依赖**: 首先,在项目中安装`dayjs`和`vue-i18n`,如果需要国际化支持。运行以下命令: ``` npm install dayjs vue-i18n @element-plus/dayjs_locale zh-cn // 如果你需要中文支持 ``` 2. **引入并使用Day.js**: 在`main.ts`或`setup()`函数中导入Day.js并导出其实例,以便在组件中使用: ```javascript import { createApp } from &#39;vue&#39; import ElementPlus from &#39;@element-plus/core&#39; import Dayjs from &#39;dayjs&#39; const app = createApp(App) app.use(ElementPlus) // 使用默认的语言环境,如果需要特定语言,可以替换为&#39;dz&#39;、&#39;zh-cn&#39;等 app.config.globalProperties.$dayjs = Dayjs(&#39;en&#39;) // 或者 &#39;zh-cn&#39; // 其他配置... app.mount(&#39;#app&#39;) ``` 3. **在模板中使用Day.js**: 在`.vue`文件中,可以直接使用`$dayjs`作为日期处理工具: ```html <template> <el-date-picker v-model="selectedDate" type="daterange" placeholder="选择日期范围"> <span>开始日期</span> <span>结束日期</span> </el-date-picker> <p>当前日期:{{ $dayjs().format(&#39;YYYY-MM-DD HH:mm:ss&#39;) }}</p> </template> <script setup> import { ref } from &#39;vue&#39; const selectedDate = ref() </script> ``` 4. **国际化的支持**: 如果你想使用中文或其他语言,可以在创建Day.js实例时指定相应的语言包,如上例中的`&#39;zh-cn&#39;`。 5.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值