当前位置:首页 > 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>

合理使用计算属性:

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

uniapp组件写法

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

相关文章

vue实现折叠组件

vue实现折叠组件

实现折叠组件的基本思路 在Vue中实现折叠组件通常需要利用动态绑定和条件渲染。核心是通过控制一个布尔值状态来决定内容是否显示,并添加过渡动画提升用户体验。 基础实现方法 使用v-show或v-if控…

vue实现递归组件

vue实现递归组件

递归组件的实现方法 在Vue中实现递归组件主要依靠组件调用自身的能力。以下是几种常见的实现方式: 使用组件name属性 通过组件的name属性实现递归调用是最简单的方式: <template…

vue实现弹窗组件

vue实现弹窗组件

实现弹窗组件的基本结构 在Vue中创建一个弹窗组件通常需要三个核心部分:组件模板、样式和逻辑控制。弹窗组件应具备打开、关闭功能,并支持内容插槽或属性传入。 <template> &l…

vue 动态组件实现

vue 动态组件实现

vue 动态组件实现 Vue 的动态组件功能允许根据条件或用户交互动态切换不同的组件,主要通过 <component> 标签和 is 属性实现。 基本用法 通过 is 属性绑定组件名或组…

vue 实现table组件

vue 实现table组件

Vue 实现 Table 组件 基础表格结构 使用 Vue 的模板语法构建表格的基本框架,通过 v-for 动态渲染数据。 <template> <table>…

vue实现图片组件

vue实现图片组件

Vue 实现图片组件的方法 基础图片组件实现 创建一个基础的图片组件,支持动态传入图片路径和替代文本。在Vue项目中新建一个ImageComponent.vue文件: <template&g…