{{ mes…">

当前位置:首页 > VUE

vue各种功能实现

2026-03-08 17:38:21VUE

Vue 基础功能实现

数据绑定
使用 v-model 实现双向数据绑定,适用于表单输入、复选框等场景:

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

数据通过 data() 定义:

data() {
  return {
    message: 'Hello Vue!'
  }
}

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

<div v-if="isVisible">显示内容</div>
<div v-show="isActive">动态显示</div>

列表渲染
v-for 渲染数组或对象:

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

Vue 组件通信

Props 传递数据
父组件通过属性传递数据:

<ChildComponent :title="parentTitle" />

子组件通过 props 接收:

props: ['title']

自定义事件
子组件通过 $emit 触发事件:

this.$emit('update', newValue)

父组件监听事件:

<ChildComponent @update="handleUpdate" />

Vuex 状态管理
定义 store:

const store = new Vuex.Store({
  state: { count: 0 },
  mutations: {
    increment(state) {
      state.count++
    }
  }
})

组件中调用:

this.$store.commit('increment')

Vue 路由功能

路由配置
通过 Vue Router 定义路径:

const routes = [
  { path: '/home', component: Home },
  { path: '/about', component: About }
]

导航守卫
全局前置守卫:

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) next('/login')
  else next()
})

Vue 高级特性

动态组件
通过 :is 切换组件:

<component :is="currentComponent"></component>

插槽(Slots)
父组件传递模板片段:

<ChildComponent>
  <template v-slot:header>
    <h1>标题内容</h1>
  </template>
</ChildComponent>

混入(Mixins)
复用逻辑:

const myMixin = {
  created() {
    this.hello()
  },
  methods: {
    hello() {
      console.log('Mixin loaded')
    }
  }
}

Vue 性能优化

异步组件
按需加载组件:

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

计算属性缓存
使用 computed 替代复杂表达式:

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

Keep-alive
缓存组件状态:

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

Vue 3 组合式 API

setup 函数
替代 datamethods

setup() {
  const count = ref(0)
  const increment = () => count.value++
  return { count, increment }
}

生命周期钩子
使用 onMounted 等函数:

vue各种功能实现

import { onMounted } from 'vue'
setup() {
  onMounted(() => {
    console.log('组件已挂载')
  })
}

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

相关文章

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…

vue 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例:…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue实现上传

vue实现上传

Vue 文件上传实现方法 使用 Vue 实现文件上传可以通过原生 HTML5 的 <input type="file"> 元素结合 Vue 的事件处理和数据绑定来实现。 基础实现步骤…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…