当前位置:首页 > VUE

用vue实现页面编程

2026-02-23 22:22:46VUE

Vue 实现页面编程的核心方法

创建 Vue 项目

使用 Vue CLI 或 Vite 快速初始化项目:

npm init vue@latest  # Vue 3 推荐
# 或
npm init vite@latest my-vue-app --template vue

组件化开发

单文件组件(.vue)结构示例:

<template>
  <div class="example">
    <h1>{{ title }}</h1>
    <button @click="handleClick">点击</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: 'Vue页面示例'
    }
  },
  methods: {
    handleClick() {
      alert('按钮被点击')
    }
  }
}
</script>

<style scoped>
.example {
  color: #42b983;
}
</style>

路由配置

安装 Vue Router 并配置:

// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: () => import('../views/About.vue') }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

状态管理

使用 Pinia(Vue 官方推荐):

// stores/counter.js
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({ count: 0 }),
  actions: {
    increment() {
      this.count++
    }
  }
})

组合式 API

Vue 3 推荐写法:

用vue实现页面编程

<script setup>
import { ref, computed } from 'vue'

const count = ref(0)
const doubleCount = computed(() => count.value * 2)

function increment() {
  count.value++
}
</script>

生命周期应用

常用生命周期钩子示例:

import { onMounted, onUnmounted } from 'vue'

onMounted(() => {
  console.log('组件已挂载')
  // 初始化操作
})

onUnmounted(() => {
  console.log('组件已卸载')
  // 清理操作
})

表单处理

双向绑定示例:

<template>
  <input v-model="message" placeholder="输入内容">
  <p>输入的内容是:{{ message }}</p>
</template>

<script setup>
const message = ref('')
</script>

API 请求

使用 axios 示例:

用vue实现页面编程

import axios from 'axios'

const fetchData = async () => {
  try {
    const response = await axios.get('/api/data')
    console.log(response.data)
  } catch (error) {
    console.error('请求出错:', error)
  }
}

响应式样式

Vue 3 的样式绑定:

<template>
  <div :class="{ active: isActive }" :style="{ color: textColor }">
    动态样式
  </div>
</template>

<script setup>
const isActive = ref(true)
const textColor = ref('#ff0000')
</script>

自定义指令

创建全局指令:

// main.js
app.directive('focus', {
  mounted(el) {
    el.focus()
  }
})

性能优化

使用 keep-alive 缓存组件:

<template>
  <keep-alive>
    <component :is="currentComponent"></component>
  </keep-alive>
</template>

标签: 页面vue
分享给朋友:

相关文章

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue 实现回复

vue 实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

vue computed实现

vue computed实现

计算属性的基本用法 在Vue中,计算属性通过computed选项定义,用于声明依赖于其他属性的动态值。计算属性基于它们的依赖进行缓存,只有依赖发生变化时才会重新计算。 new Vue({ dat…

vue实现getapp

vue实现getapp

Vue 中实现全局获取应用实例的方法 在 Vue 3 中,可以通过 getCurrentInstance 方法获取当前组件实例,但需要明确其使用场景和限制。以下是具体实现方式: 方法一:通过 get…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…