{{ 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:

vue各种功能实现

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)
父组件传递模板片段:

vue各种功能实现

<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 等函数:

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

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

相关文章

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 win…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue实现swipe

vue实现swipe

Vue实现Swipe功能的方法 使用第三方库(推荐) Vue生态中有多个成熟的轮播/滑动组件库,例如vue-awesome-swiper或swiper/vue。以下是基于swiper/vue的实现示例…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: impo…