uniapp全局或局部配置分享

uniapp自定义全局分享好友


vue mixins


创建mixins目录

  • 在项目根目录下创建mixins目录
  • 新建share.js文件
export default {
    data() {
       return {
            shareData: {
               title: '好东西一起分享~~~',
               imageUrl: '',
            },
       }
    },
	onLoad() {
		let tmpUserId = this.$store.state.vuex_userInfo.id;
		this.invite = tmpUserId ? tmpUserId : '';
      	// this.shareData = this.$store.state.commShareData;
	},
    onShareAppMessage(res) { //发送给朋友
        return {
            title: this.shareData.shareTitle, // 
            imageUrl: this.shareData.sharePosterUrl, 
			path: 'pages/index/index' + this.invite
        }
    },
    onShareTimeline(res) { //分享到朋友圈
        return {
            title: this.shareData.title,
            imageUrl: this.shareData.imageUrl,
			path: 'pages/index/index?inviteCode=' + this.invite
        }
    }
}

使用

全局引入

main.js中引入

import { share } from './mixins/share.js'
Vue.mixin(share)

局部引入

在需要分享的页面引入

import mixin from '../../mixins/share.js'

export default {
	mixins: [mixin],
}
在 uni-app 中,使用 `components` 时需要在 `pages.json` 文件中配置 `usingComponents` 字段。`usingComponents` 字段用来声明在这个页面中要使用的自定义组件,可以是全局注册的组件,也可以是局部注册的组件。 例如,在 `pages/index/index.vue` 页面中,我们要使用一个名为 `my-component` 的自定义组件,可以在 `pages.json` 文件中添加以下配置: ```json { "pages": [ { "path": "pages/index/index", "usingComponents": { "my-component": "/components/my-component/index" } } ] } ``` 在上面的配置中,我们将 `my-component` 组件的路径设置为 `/components/my-component/index`,这表示 `my-component` 组件的定义文件在 `components/my-component/index.vue` 中。 之后,在 `pages/index/index.vue` 页面中就可以直接使用 `my-component` 组件了: ```vue <template> <div> <my-component></my-component> </div> </template> ``` 在 `pages.json` 文件中可以配置多个页面,每个页面都可以声明自己要使用的自定义组件。 需要注意的是,如果使用的是 npm 包中的组件,在 `usingComponents` 中的路径应该使用 `npm://` 前缀,例如: ```json { "pages": [ { "path": "pages/index/index", "usingComponents": { "my-component": "npm://my-component" } } ] } ``` 在上面的配置中,我们使用 `npm://` 前缀来表示 `my-component` 组件是从 npm 包中引入的。实际使用时,需要将 `npm://` 替换为 `/@npm/`,例如: ```json { "pages": [ { "path": "pages/index/index", "usingComponents": { "my-component": "/@npm/my-component" } } ] } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值