当前位置:首页 > uni-app

uniapp组件写法

2026-02-05 17:30:38uni-app

uniapp组件的基本结构

在uniapp中创建组件通常需要遵循Vue单文件组件(SFC)规范。一个典型的组件文件包含<template><script><style>三个部分。

<template>
  <view class="example-component">
    <text>{{ message }}</text>
  </view>
</template>

<script>
export default {
  name: 'ExampleComponent',
  props: {
    msg: {
      type: String,
      default: 'Hello'
    }
  },
  data() {
    return {
      message: this.msg
    }
  },
  methods: {
    updateMessage(newMsg) {
      this.message = newMsg
    }
  }
}
</script>

<style scoped>
.example-component {
  padding: 20px;
}
</style>

组件的注册与使用

全局注册通常在main.js中进行:

import Vue from 'vue'
import ExampleComponent from '@/components/ExampleComponent.vue'

Vue.component('example-component', ExampleComponent)

局部注册在页面或父组件中进行:

<script>
import ExampleComponent from '@/components/ExampleComponent.vue'

export default {
  components: {
    ExampleComponent
  }
}
</script>

使用组件:

<template>
  <view>
    <example-component msg="Welcome"></example-component>
  </view>
</template>

组件间的通信方式

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

<child-component :propName="parentData"></child-component>

子组件向父组件发送事件:

// 子组件
this.$emit('custom-event', payload)

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

插槽的使用

默认插槽:

<!-- 子组件 -->
<view>
  <slot></slot>
</view>

<!-- 父组件 -->
<child-component>
  <text>默认插槽内容</text>
</child-component>

具名插槽:

<!-- 子组件 -->
<view>
  <slot name="header"></slot>
  <slot name="footer"></slot>
</view>

<!-- 父组件 -->
<child-component>
  <template v-slot:header>
    <text>头部内容</text>
  </template>
  <template v-slot:footer>
    <text>底部内容</text>
  </template>
</child-component>

生命周期钩子

uniapp组件支持Vue的所有生命周期钩子:

export default {
  beforeCreate() {},
  created() {},
  beforeMount() {},
  mounted() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  // uniapp特有生命周期
  onLoad() {},
  onShow() {},
  onReady() {},
  onHide() {},
  onUnload() {}
}

样式作用域

使用scoped属性可限制样式作用域:

<style scoped>
/* 仅作用于当前组件 */
</style>

条件与循环渲染

条件渲染:

<view v-if="show">显示内容</view>
<view v-else>备选内容</view>

循环渲染:

<view v-for="(item, index) in items" :key="index">
  {{ item.text }}
</view>

表单组件绑定

使用v-model实现双向绑定:

<input v-model="inputValue" type="text">
<checkbox-group v-model="checkedValues">
  <checkbox value="option1"></checkbox>
  <checkbox value="option2"></checkbox>
</checkbox-group>

自定义事件处理

组件内部触发事件:

methods: {
  handleClick() {
    this.$emit('click', eventData)
  }
}

外部监听事件:

<custom-button @click="handleButtonClick"></custom-button>

组件性能优化

使用v-once进行静态内容优化:

<text v-once>永不改变的内容</text>

合理使用计算属性:

uniapp组件写法

computed: {
  filteredList() {
    return this.list.filter(item => item.active)
  }
}

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

相关文章

vue实现广告组件

vue实现广告组件

Vue 实现广告组件的核心方法 数据驱动的广告内容渲染 通过 props 接收广告数据(如图片URL、跳转链接等),使用 v-bind 动态绑定属性。典型结构包含 <a> 标签嵌套 <…

vue实现倒计时组件

vue实现倒计时组件

Vue 倒计时组件实现 核心思路 通过 setInterval 或 setTimeout 实现时间递减,结合 Vue 的响应式数据更新 UI。需注意组件销毁时清除定时器。 基础实现方案 模板部分…

vue组件实现跳转

vue组件实现跳转

Vue 组件实现跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,以下是常见的几种方法: 使用 router-link 组件 router-link 是 Vue Router 提供的组件,用于…

vue 缩放组件实现

vue 缩放组件实现

实现 Vue 缩放组件的核心方法 使用 CSS transform 实现基础缩放 通过动态绑定 transform: scale() 样式实现缩放效果。在 Vue 组件的模板中添加需要缩放的元素,并通…

vue组件实现tab

vue组件实现tab

Vue 组件实现 Tab 切换 基础实现方案 模板结构 使用 v-for 渲染标签页标题,通过 v-show 或 v-if 控制内容显示: <template> <div cl…

vue组件如何实现

vue组件如何实现

Vue 组件实现方法 Vue 组件可以通过多种方式实现,以下是常见的几种方法: 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template>…