当前位置:首页 > uni-app

uniapp组件写法

2026-01-13 19:22:03uni-app

Uniapp 组件的基本写法

Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template><script><style>

<template>
  <view class="my-component">
    <text>{{ message }}</text>
    <button @click="handleClick">点击我</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello Uniapp!'
    }
  },
  methods: {
    handleClick() {
      console.log('按钮被点击了')
    }
  }
}
</script>

<style>
.my-component {
  padding: 20px;
}
button {
  margin-top: 10px;
}
</style>

组件注册与使用

在 Uniapp 中,组件分为全局注册和局部注册两种方式。

全局注册main.js 中注册组件,使其在整个应用中可用:

import Vue from 'vue'
import MyComponent from './components/MyComponent.vue'

Vue.component('my-component', MyComponent)

局部注册 在需要使用组件的页面中引入:

import MyComponent from '../../components/MyComponent.vue'

export default {
  components: {
    MyComponent
  }
}

组件间通信

Uniapp 组件间通信主要有以下几种方式:

uniapp组件写法

Props 传递数据 父组件向子组件传递数据:

<!-- 父组件 -->
<child-component :msg="parentMessage"></child-component>

<!-- 子组件 ChildComponent.vue -->
<script>
export default {
  props: {
    msg: String
  }
}
</script>

$emit 触发事件 子组件向父组件发送事件:

<!-- 子组件 -->
<button @click="$emit('custom-event', payload)">触发事件</button>

<!-- 父组件 -->
<child-component @custom-event="handleEvent"></child-component>

使用 Vuex 状态管理 对于复杂应用,建议使用 Vuex 进行状态管理:

uniapp组件写法

// store.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  }
})

// 组件中使用
this.$store.commit('increment')
console.log(this.$store.state.count)

生命周期钩子

Uniapp 组件支持 Vue 的生命周期钩子,同时还扩展了一些应用特有的生命周期:

export default {
  onLoad() {
    // 页面加载时触发
  },
  onShow() {
    // 页面显示时触发
  },
  mounted() {
    // 组件挂载完成
  },
  beforeDestroy() {
    // 组件销毁前
  }
}

样式作用域

使用 scoped 属性可以限制样式只作用于当前组件:

<style scoped>
/* 这些样式只作用于当前组件 */
.my-class {
  color: red;
}
</style>

组件插槽

Uniapp 支持 Vue 的插槽机制,用于内容分发:

<!-- 父组件 -->
<my-component>
  <template v-slot:default>默认内容</template>
  <template v-slot:footer>页脚内容</template>
</my-component>

<!-- 子组件 MyComponent.vue -->
<view>
  <slot></slot>
  <view class="footer">
    <slot name="footer"></slot>
  </view>
</view>

注意事项

  • 组件名应使用 kebab-case(短横线分隔命名)
  • 避免在组件中使用 id 选择器,因为 Uniapp 会编译到不同平台
  • 组件中的 v-for 循环需要指定唯一的 key
  • 在小程序平台,组件样式可能会受到更多限制

标签: 写法组件
分享给朋友:

相关文章

vue如何实现目录组件

vue如何实现目录组件

Vue 目录组件实现方法 基于递归组件实现树形目录 递归组件适合处理嵌套层级不确定的目录结构。创建一个可自调用的组件,通过 v-for 遍历子节点并递归渲染自身。 <template&g…

vue实现组件

vue实现组件

Vue 组件实现基础 Vue 组件是可复用的 Vue 实例,用于封装 UI 和逻辑。通过 .vue 文件或直接注册组件实现。 单文件组件 (SFC) 示例 <template>…

vue轮播组件实现

vue轮播组件实现

Vue 轮播组件实现方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&g…

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性,或者使用第三方 UI 库…

vue动态组件实现

vue动态组件实现

Vue 动态组件实现方法 使用 component 标签与 is 属性 通过 Vue 内置的 <component> 标签和 is 属性实现动态组件切换。is 的值可以是已注册的组件名或导…

vue组件的实现

vue组件的实现

Vue 组件的实现方式 Vue 组件可以通过多种方式实现,主要包括单文件组件(SFC)、全局注册和局部注册。以下是常见的实现方法: 单文件组件(SFC) 单文件组件是 Vue 最推荐的组件化开发方式…