在uniapp中怎么设置公共组件供全局使用

在uniapp的components的文件中新建一个文件夹,然后在vue的文件中写上需要的组件,然后在main.js中引用。

这样就可以在全局中使用了,比如在使用忘记密码是

在下面使用ref使用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提的引用内容,uniapp全局弹窗组件可以通过以下步骤实现: 1.在项目创建一个公共组件,可以命名为invite.vue,组件的结构和样式可以参考提的引用。 2.在main.js导入invite.js,并安装插件Vue.use(invite)。 3.在需要弹出组件的任何组件内调用this.$openInvite()即可弹出组件,调用this.$closeInvite()即可关闭组件。 下面是一个范例代码,参考: 1. invite.vue组件代码: ```html <template> <div class="invite-mask" v-show="isShow"> <div class="invite-container"> <div class="invite-header"> <span class="invite-title">邀请好友</span> <img class="invite-close" src="./static/images/close.png" @click="closeInvite" /> </div> <div class="invite-content"> <img class="invite-img" src="./static/images/invite.png" /> <p class="invite-text">邀请好友注册,双方都可获得优惠券</p> <button class="invite-btn" @click="closeInvite">知道了</button> </div> </div> </div> </template> <script> export default { data() { return { isShow: false } }, methods: { openInvite() { this.isShow = true }, closeInvite() { this.isShow = false } } } </script> <style> .invite-mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); z-index: 999; } .invite-container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 80%; max-width: 400px; background-color: #fff; border-radius: 10px; overflow: hidden; } .invite-header { display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f5f5f5; } .invite-title { font-size: 16px; font-weight: bold; } .invite-close { width: 20px; height: 20px; cursor: pointer; } .invite-content { display: flex; flex-direction: column; align-items: center; padding: 20px; } .invite-img { width: 80%; margin-bottom: 20px; } .invite-text { font-size: 14px; color: #666; margin-bottom: 20px; } .invite-btn { width: 80%; height: 40px; line-height: 40px; text-align: center; background-color: #ff9900; color: #fff; border-radius: 20px; cursor: pointer; } </style> ``` 2. invite.js代码: ```javascript import Invite from '@/components/invite' const install = function(Vue) { Vue.component('Invite', Invite) Vue.prototype.$openInvite = function() { this.$refs.invite.openInvite() } Vue.prototype.$closeInvite = function() { this.$refs.invite.closeInvite() } } export default { install } ``` 3. main.js代码: ```javascript import Vue from 'vue' import App from './App' import Invite from './utils/invite' Vue.use(Invite) Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ ...App }) app.$mount() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值