{…">

当前位置:首页 > VUE

vue各种功能实现

2026-03-29 04:41:18VUE

Vue 核心功能实现

双向数据绑定
通过 v-model 指令实现表单元素与数据的双向同步。例如文本输入框:

<input v-model="message">
<p>{{ message }}</p>

数据变化会自动更新视图,用户输入也会同步到数据。

条件渲染
使用 v-ifv-show 控制元素显示:

<div v-if="isVisible">仅当isVisible为true时渲染</div>
<div v-show="hasData">通过CSS切换显示</div>

v-if 是动态添加/移除DOM,v-show 仅切换 display 属性。

列表渲染与事件处理

动态列表
v-for 指令渲染数组或对象:

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

:key 是必须的,用于高效更新虚拟DOM。

事件绑定
通过 v-on(简写 @)监听事件:

<button @click="handleClick">点击</button>
<button @click.prevent="submit">阻止默认事件</button>

修饰符如 .prevent 可替代 event.preventDefault()

组件化开发

组件定义
单文件组件(SFC)结构:

vue各种功能实现

<template>
  <div>{{ title }}</div>
</template>
<script>
export default {
  props: ['title'],
  data() { return { localData: 1 } }
}
</script>

组件通信

  • 父传子:通过 props
    <ChildComponent :propName="parentData" />
  • 子传父:通过 $emit
    this.$emit('event-name', payload)

状态管理(Vuex)

核心概念

const store = new Vuex.Store({
  state: { count: 0 },
  mutations: {
    increment(state) { state.count++ }
  },
  actions: {
    asyncIncrement({ commit }) {
      setTimeout(() => commit('increment'), 1000)
    }
  }
})

组件中通过 this.$store.commit('increment') 触发变更。

路由管理(Vue Router)

基础配置

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
]
const router = new VueRouter({ routes })

导航方式:

vue各种功能实现

<router-link to="/about">跳转</router-link>

或编程式导航:

this.$router.push('/about')

高级特性

自定义指令
全局注册指令:

Vue.directive('focus', {
  inserted(el) { el.focus() }
})

使用方式:

<input v-focus>

混入(Mixins)
复用逻辑:

const myMixin = {
  created() { console.log('混入生命周期') }
}
new Vue({ mixins: [myMixin] })

性能优化

异步组件
按需加载组件:

const AsyncComponent = () => ({
  component: import('./AsyncComponent.vue'),
  loading: LoadingComponent
})

Keep-alive
缓存组件状态:

<keep-alive>
  <component :is="currentTab"></component>
</keep-alive>

标签: 功能vue
分享给朋友:

相关文章

vue实现年历

vue实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件,…

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue登录实现

vue登录实现

Vue 登录实现 实现登录功能通常需要前端与后端配合,Vue 作为前端框架,主要负责表单处理、请求发送和状态管理。以下是常见的实现方式: 表单设计与数据绑定 使用 Vue 的 v-model 实现表…

vue实现menu

vue实现menu

Vue 实现 Menu 的方法 使用 Element UI 的 Menu 组件 Element UI 提供了现成的 Menu 组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用…