uniapp 微信小程序异步调用分包的自定义组件

需求:在分包subTainxin中创建自定义组件moreFun.vue,需要在主包的pages/index/index页面调用该自定义组件。

步骤流程:

1、在分包subTainxin中创建自定义组件moreFun.vue

<template>
	<view>
		<view v-for="(item,index) in funcList" :key="index" class="item_style">
			<view class="flex_center">
				<view class="order_flag"></view><span>{{item}}</span>
				<image src="../../static/images/drag.png" class="order_img flex_right"></image>
			</view>
			<view>
				
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataList:[],
				funcList:["胎心","血氧","体重"]
			}
		},
	}
</script>

<style>
	.item_style {
		background-color: #ffffff;
		border-radius: 24rpx;
		height: 96rpx;
		margin: 24rpx 32rpx;
		padding:22rpx 24rpx
	}
	.flex_center {
		display: flex;
		align-items: center;
	}
	.order_flag {
		width:32rpx;height:32rpx; background-color: blue; border-radius: 36rpx; margin-right: 10rpx;
	}
	.order_img {
		width:42rpx;height:32rpx
	}
	.flex_right {
		position:absolute;
		right:48rpx;
	}
	
</style>

2、在主包中调用:

2.1 在pages/index/index.vue中

<template>
	<index-pressure v-if="headerLists[changeText]=='血压'" :chartData="chartDataPressure"
				:changeText="changeTextPressure" :lastRecord="lastRecord_pressure" :chartSum="chartSumPressure"
				:stepData="stepData" :measureStagePressure="measureStagePressure"></index-pressure>
	<more-fun v-else></more-fun>
</template>
<script>
	import indexPressure from '../indexPressure/indexPressure.vue'
	import moreFun from '../../subTaixin/measureHeart/moreFun.vue'
	export default {
		components: {
			indexPressure,
			moreFun,
		},
    }
</script>

2.2 在pages.json中配置

"pages": [
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "首页",
				"navigationStyle": "custom",
                //设置占位组件
				"componentPlaceholder": {
					"more-fun":"view"
				}
			}
		},
]

编译之后,在微信开发者工具中显示的信息为:

pages/index/index.json中

{
  "navigationBarTitleText": "首页",
  "navigationStyle": "custom",
  "componentPlaceholder": {
    "more-fun": "view"
  },
  "usingComponents": {
    "index-pressure": "/pages/indexPressure/indexPressure",
    "more-fun": "/subTaixin/measureHeart/moreFun"
  }
}

在微信开发者工具的模拟器上测试,能调用成功,显示正常,但真机调试时,组件不显示,控制台报错Component is not found in path "subTaixin/measureHeart/moreFun" (using by "pages/index/index");

上传到体验版上,扫码测试时又正常。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp 是一个基于 Vue.js 的跨平台开发框架,可以用来开发微信小程序、H5、App等多个平台的应用。在 UniApp 中,可以通过自定义组件来实现对小程序的扩展和复用。 要创建一个自定义组件,可以按照以下步骤进行: 1. 在 UniApp 项目的 `components` 目录下创建一个新的文件夹,用于存放自定义组件的相关文件。 2. 在该文件夹下创建一个 `.vue` 文件,作为自定义组件的入口文件。 3. 在入口文件中定义组件的模板、样式和逻辑代码。 4. 在需要使用自定义组件的页面或组件中,引入并使用自定义组件。 下面是一个简单的示例,演示如何创建一个自定义组件: 1. 在 `components` 目录下创建一个名为 `my-component` 的文件夹。 2. 在 `my-component` 文件夹下创建一个名为 `my-component.vue` 的文件,作为自定义组件的入口文件。 3. 在 `my-component.vue` 文件中编写如下代码: ```html <template> <view class="my-component"> <text>{{ message }}</text> </view> </template> <script> export default { name: 'MyComponent', props: { message: { type: String, default: '' } } } </script> <style scoped> .my-component { color: red; } </style> ``` 4. 在需要使用自定义组件的页面或组件中,引入并使用自定义组件。例如,在某个页面的 `.vue` 文件中: ```html <template> <view> <my-component message="Hello World"></my-component> </view> </template> <script> import MyComponent from '@/components/my-component/my-component.vue' export default { components: { MyComponent } } </script> ``` 这样,就可以在页面中使用名为 `my-component` 的自定义组件了。 需要注意的是,自定义组件的文件夹和文件名可以根据实际情况进行命名,但要保持一致性。另外,自定义组件中的样式可以使用 `scoped` 关键字来实现样式的隔离,避免与其他组件的样式冲突。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值