Master Uniapp Vue Core Directives in 5 Minutes – Hands‑On Guide
This tutorial walks you through Uniapp's essential Vue syntax, covering data binding, event handling, conditional and list rendering, with a complete code example and common pitfalls to help you build a simple personal information page quickly.
Key Operations (Must‑Read)
1. Core Vue Directives (Only Four Needed)
Data binding: {{ }} – renders variables.
Event binding: @click – handles click interactions.
Conditional rendering: v-if – shows or hides content based on a condition.
List rendering: v-for – iterates over arrays, e.g., a product list.
2. Hands‑On Scenario: Build a Simple Personal Info Page
Open the my.vue page created in the previous series.
Define data in the script section using a data() function.
Use the directives in the template to render data and bind events.
Run the project, observe the result, and modify the code with live debugging.
Core Code (Copy‑Paste Ready)
<template>
<view class="my-container">
<!-- 1. Data binding: render variables -->
<view class="info">
<text class="title">Personal Info</text>
<view>Name: {{ name }}</view>
<view>Age: {{ age }}</view>
</view>
<!-- 2. Event binding: click button to change age -->
<button @click="changeAge">Change Age</button>
<!-- 3. Conditional rendering: show adult if age >= 18 -->
<view v-if="age >= 18" class="adult">Adult</view>
<view v-else class="minor">Minor</view>
<!-- 4. List rendering: iterate hobbies -->
<view class="hobby">
<text>My hobbies:</text>
<view v-for="(item, index) in hobby" :key="index">
{{ index+1 }}. {{ item }}
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
name: "Zhang San", // define variable
age: 18,
hobby: ["Uniapp development", "Frontend learning", "Food"] // define array
};
},
methods: {
// define click handler
changeAge() {
this.age += 1; // increment age variable
}
}
};
</script>
<style scoped>
.my-container { padding: 20rpx; }
.info { margin-bottom: 20rpx; }
.title { font-size: 32rpx; font-weight: bold; margin-bottom: 10rpx; display: block; }
button { width: 100%; height: 80rpx; background-color: #007aff; color: #fff; margin: 20rpx 0; }
.adult { color: green; }
.minor { color: red; }
.hobby { margin-top: 20rpx; }
</style>Common Pitfalls
❌ Data binding not working: ensure the variable is defined in data() and the name inside {{ }} matches exactly.
❌ Click event not working: verify the method name matches the one defined in methods; using @click is preferred over v-on:click for simplicity.
❌ v-for errors: always provide a :key attribute (e.g., the index) to avoid rendering issues.
✅ Uniapp templates must have a single root view element; all content should be placed inside that root.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
liandk
Seasoned Java and mobile developer with years of experience, specializing in mini‑programs, public accounts, and full‑stack front‑end development. In the AI era, I continuously learn to broaden my knowledge and evolve. I revived a public account I started a decade ago during a dessert‑startup venture, using code as a vessel and knowledge as a companion. I share personal projects, technical articles, programming tips, and growth insights—let’s improve together and set sail.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
