{{ 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怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$router…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…