陪诊小程序之uniapp(从入门到精通)

1.uniapp如何使用vue3编写页面

复制代码
<template>
	<view class="content">
		<navbar name="navbar组件"></navbar>
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<view class="">
			11111
		</view>
		<button @click="handleClick">点我</button>
		<text>总共购买的水果数量{{totalNum}}</text>
		<my-component></my-component>
		<aComponent></aComponent>
		<navbar></navbar>
		<view v-for="item in list" :key="item.name">
			<view>
				1111
			</view>
			<text>{{item.name}}</text>
			<text>{{item.num}}</text>
		</view>
	</view>
</template>

<script setup>
	import  aComponent  from '../../../project/component/component.vue';
	
	import{ref,reactive,computed} from 'vue'
	import{onLoad} from '@dcloudio/uni-app'
	const title=ref('Hello')
	const list=reactive([
		{name:'apple',num:1},
		{name:'orange',num:2},
		{name:'banana',num:3}
	])
	const handleClick=()=>{
		list.forEach(item=>{
			item.num++
		})
	}
	onLoad(()=>{
		console.log('onLode生命周期')
	})
	const totalNum=computed(()=>{
		return list.reduce((total,cur)=>total+cur.num,0)
	})
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

引入组件的三种方式

全局引入

1.引入

import componentVue from './component/component.vue'

2.全局注册

export function createApp() {

const app = createSSRApp(App)
app.component('my-component',componentVue)

return {

app

}

}

3.页面中引入

<my-component></my-component>

局部引入

<script setup>

import aComponent from '../../../project/component/component.vue';

import{ref,reactive,computed} from 'vue'

import{onLoad} from '@dcloudio/uni-app'

const title=ref('Hello')

const list=reactive([

{name:'apple',num:1},

{name:'orange',num:2},

{name:'banana',num:3}

])

const handleClick=()=>{

list.forEach(item=>{

item.num++

})

}

onLoad(()=>{

console.log('onLode生命周期')

})

const totalNum=computed(()=>{

return list.reduce((total,cur)=>total+cur.num,0)

})

</script>

复制代码
<aComponent></aComponent>

自动引入

新建文件夹然后新建组件

组件引入

复制代码
	<navbar></navbar>

2.uniapp组件通信props和$emit和插槽语法

props

组件

复制代码
<template>
	<view>
		navbar组件
	</view>
</template>

<script setup>
import { defineProps } from 'vue';
defineProps(['name','content'])

</script>

<style>

</style>

组件引入数据

复制代码
<navbar name="navbar组件" :content="data"></navbar>

父组件(页面)向子组件传值,如果没传就用默认值

复制代码
<template>
	<view>
		navbar组件
		<view>组件的name属性{{name}}</view>
		<view>组件的content属性{{content}}</view>
		
	</view>
</template>

<script setup>
import { defineProps } from 'vue';
// defineProps(['name','content'])
defineProps({
name:String,	
	content:{
		type:String,
		default:()=>{
			return '默认值';
		}
	}	
})

</script>

<style>

</style>

$emit

navbar.vue

<template>

<view>

navbar组件

<view>组件的name属性{{name}}</view>
<view>组件的content属性{{content}}</view>

<button @click="handleChange">修改content</button>

</view>

</template>

<script setup>

import { defineProps,defineEmits } from 'vue';

// defineProps(['name','content'])

defineProps({

name:String,

content:{

type:String,

default:()=>{

return '默认值';

}

}

})
const emit=defineEmits(['changeData'])
const handleChange=()=>{
emit('changeData','修改后的数据')
}

</script>

<style>

</style>
<navbar :name="navbar组件" :content="data" @changeData="changeData"></navbar>

插槽语法

<navbar :name="navbar组件" :content="data" @changeData="changeData">

<view>我是插槽的内容</view>

</navbar>
<template>

<view>组件</view>

<view>组件的name属性{{name}}</view>

<view>组件的content属性{{content}}</view>

<slot></slot>

<button @click="handleChange">修改content</button>

</template>

<script setup>

import { defineProps,defineEmits } from 'vue';

// defineProps(['name','content'])

defineProps({

name:String,

content:{

type:String,

default:()=>{

return '默认值';

}

}

})

const emit=defineEmits(['changeData'])

const handleChange=()=>{

emit('changeData','修改后的数据')

}

</script>

<style>

</style>

相关推荐
fakaifa5 小时前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_9159184112 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
上海云盾第一敬业销售17 小时前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序
suncentwl18 小时前
做一个答题pk小程序多少钱?
小程序·答题小程序·知识竞赛·答题pk软件
说私域19 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化策略研究
人工智能·小程序
咸虾米_19 小时前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
微信小程序·小程序·uniapp开发·小程序地图api
梦里寻码20 小时前
自行食用 uniapp 多端 手写签名组件
前端·uni-app
未来之窗软件服务20 小时前
蔬菜批发小程序:生产商的数字化转型利器——仙盟创梦IDE
小程序·自动化·仙盟创梦ide·东方仙盟·蔬菜批发·批发系统
数据皮皮侠1 天前
最新上市公司业绩说明会文本数据(2017.02-2025.08)
大数据·数据库·人工智能·笔记·物联网·小程序·区块链
不如摸鱼去2 天前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
微信小程序·小程序·uni-app·aigc·ai编程