Vue message组件开发思路

很多时候,在开发项目的时候是需要抽离公共组件和业务组件的。而有些公共组件在全局注册的同时可能还需要拓展成vue的实例方法,通过把它们添加到 Vue.prototype 上实现,方便直接使用js全局调用。拿一个Message组件做例子吧,代码比较简单,就直接上代码了。

  1. 首先开发好Message.vue文件。
<template>
  <div class='y-mask-white-dialog' v-show='show'>
    <div class='y-message  animated zoomIn' >
      <span v-html="msg"></span>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'yMessage',
  props: {
    msg: String,
    timeout: Number,
    callback: Function,
    icon: String,
  },
  data() {
    return {
      show: true,
    };
  }
};
</script>
 
<style lang="stylus"  scoped>
.y-mask-white-dialog {
  background-color: rgba(0, 0, 0, .4);
  position: fixed;
  z-index: 999;
  bottom: 0;
  right: 0;
  left: 0;
  top: 0;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
}
.y-message {
  min-width: 2.9rem;
  max-width: 5.5rem;
  width:100%;
  padding: 0.32rem;
  font-size: 14px;
  text-align: center;
  border-radius: 4px;
  background :rgba(0,0,0,0.8);
  color: #fff;
  animation: zoomIn .15s ease forwards;
}
</style>
  1. 构建Message的Constructor
import Vue from 'vue';
 
const MsgConstructor = Vue.extend(require('./Message.vue'));
 
const instance = new MsgConstructor({
  // el: document.createElement('div'),
}).$mount(document.createElement('div'));
 
MsgConstructor.prototype.closeMsg = function () {
  const el = instance.$el;
  el.parentNode && el.parentNode.removeChild(el);
  typeof this.callback === 'function' && this.callback();
};
 
const Message = (options = {}) => {
  instance.msg = options.msg;
  instance.timeout = options.timeout || 2000;
  instance.icon = options.icon;
  instance.callback = options.callback;
  document.body.appendChild(instance.$el);
 
  const timer = setTimeout(() => {
    clearTimeout(timer);
    instance.closeMsg();
  }, instance.timeout + 100);
};
 
export default Message;
  1. 在main.js里面进行组件注册
import Message from './components/Message';
 
Vue.component(Message.name, Message)
Vue.prototype.$message = Message

然后你就可以尽情使用Message组件了


this.$message({
  msg: 'test message'
  // ...
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue组件开发是一种将页面拆分成独立、可复用的组件开发方式。下面是Vue组件开发的一般过程: 1. 创建组件:首先,你需要创建一个Vue组件。一个Vue组件通常由模板、脚本和样式组成。模板定义了组件的结构和布局,脚本定义了组件的行为和数据,样式定义了组件的外观。 2. 注册组件:在使用组件之前,你需要将其注册到Vue实例中。可以通过全局注册或局部注册的方式来注册组件。全局注册可以在任何地方使用该组件,而局部注册只能在特定的Vue实例中使用。 3. 使用组件:注册完成后,你可以在模板中使用该组件。通过在模板中使用组件的标签,就可以将该组件渲染到页面上。 4. 传递数据:在使用组件时,你可以通过props属性向组件传递数据。props属性允许父组件向子组件传递数据,并且子组件可以在脚本中使用这些数据。 5. 监听事件:除了传递数据,你还可以在子组件中触发事件,并在父组件中监听这些事件。通过自定义事件,可以实现组件与父组件之间的通信。 6. 组件间通信:有时候,你可能需要在不相关的组件之间进行通信。Vue提供了多种方式来实现组件间的通信,如使用事件总线、Vuex状态管理等。 7. 组件复用:组件开发的一个重要优势是组件的复用性。你可以在不同的项目中重复使用已经创建好的组件,提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值