[Share] The Hidden Gems of HarmonyOS Development! A Hands-on Guide to "Develop Once, Deploy Everywhere" Practical Skills!
Hello everyone~ Today, while browsing the HarmonyOS developer documentation, I discovered a super practical collection of "One-for-Many" development cases! It turns out that the official team has already prepared various cross-device adaptation solutions—like discovering a new continent! I quickly organized and am sharing several highly practical mobile payment development cases to help you avoid detours~
The Magic of Interface Layout: Grid System
King Kong Area Transformation
Mobile: Circular icon + vertical text
Large screen: Rounded rectangle + horizontal layout
Key code:
GridRow({gutter: {x: {sm:30, md:41, lg:58}}}) {
ForEach(this.quickFunctions, (item) => {
GridCol({span:3}) {
// Switch component shape based on screen size
this.curBp === 'sm' ?
<CircularComponent> : <RectangularComponent>
}
})
}
Practical tip: Use the span property of GridCol to control element proportions, and combine with breakpoint listeners to achieve "smart layout."
- The Seventy-Two Changes of Function Entry
- Mobile shows 4 columns → Tablet 6 columns → PC 8 columns Secret weapon: Dynamic configuration of the columns property
GridRow({
columns: {sm:4, md:6, lg:8}, // The magic numbers are here!
gutter: {x:{sm:45, md:50, lg:55}}
})
The Cross-Device Secrets of Payment and Collection
(with pop-up and full-screen page switch diagrams)Intelligent Device Size Detection:
private receivePayment() {
if (this.curBp === 'sm') {
// On mobile, navigate to a new page
router.pushUrl({url:'ReceivePaymentPage'})
} else {
// On large screens, show a dialog
this.isDialogOpen = true
}
}
- The Pitfalls and Solutions of Dynamic QR Codes:
// Timed refresh logic
aboutToAppear() {
this.timer = setInterval(() => {
this.getNewQRCode() // Call API to update
}, 60000)
}
// Must remember to clear!
aboutToDisappear() {
clearInterval(this.timer)
}
Pitfall tip: Remember to listen for breakpoint changes when folding large screens, or layout issues may occur!
Cross-Device Adaptation for Scan Feature
Adaptive Camera Area:
// Responsive design using percentages
Scanner({
width: '70%',
height: '70%',
aspectRatio: 1 // Force 1:1 ratio
})
- Third-Party Payment Page Adaptation:
- Mobile: Full-screen Web component
- PC: Embedded iframe + independent operation area
if (breakpoint === 'lg') {
this.useIframeMode = true
}
Card Pack Module Layout Secrets
Card Waterfall Layout:
GridCol({
span: {sm:12, md:6, lg:4} // Show 1/2/3 columns on three devices respectively
}) {
BankCardComponent()
}
- Interaction Differences for Adding Bank Cards:
- Mobile: Bottom pop-up
- Tablet: Slide out from the right
- PC: Centered dialog
promptAction.showModal({
alignment: deviceType === 'phone' ?
Alignment.Bottom : Alignment.Center
})
Development Tips
Breakpoint listeners should be written in the aboutToAppear lifecycle
Use the Blank component to fill blank areas more flexibly
Make good use of the @Extend decorator to reuse styles
Multi-device preview shortcut: Ctrl+Shift+M
Conclusion:
These official cases are like martial arts secrets—once mastered, you can truly "write code once and automatically adapt to all devices!" I recommend creating a new project in your IDE and trying out these code snippets yourself. You'll definitely feel like you've unlocked a new level of skill~
If you want to see analysis for other vertical scenarios (such as e-commerce, social apps), feel free to leave a comment! More HarmonyOS development tips will be shared in the future, so remember to follow~ ✨
Top comments (0)