当前位置:首页 > VUE

vue页面实现

2026-03-07 02:18:49VUE

Vue 页面实现基础步骤

创建 Vue 页面需要安装 Vue 环境、编写组件模板、定义逻辑与样式。以下是具体实现方式:

环境准备 确保已安装 Node.js 和 npm/yarn,通过 Vue CLI 或 Vite 初始化项目:

npm init vue@latest  # Vue CLI
npm create vite@latest  # Vite

单文件组件结构 Vue 页面通常以 .vue 文件组织,包含 <template>, <script>, <style> 三个部分:

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

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

<style scoped>
.page-container {
  padding: 20px;
}
</style>

路由配置实现多页面

安装 Vue Router

npm install vue-router

路由定义示例src/router/index.js 中配置路径与组件映射:

import { createRouter, createWebHistory } from 'vue-router';
import HomePage from '../views/HomePage.vue';
import AboutPage from '../views/AboutPage.vue';

const routes = [
  { path: '/', component: HomePage },
  { path: '/about', component: AboutPage }
];

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

export default router;

路由视图占位 在根组件 App.vue 中添加 <router-view>

<template>
  <router-view />
</template>

状态管理(Vuex/Pinia)

Pinia 基础用法 安装 Pinia 并创建 store:

npm install pinia

定义 Store

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

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

在组件中使用

<script setup>
import { useCounterStore } from '@/stores/counter';

const counter = useCounterStore();
</script>

<template>
  <button @click="counter.increment">
    {{ counter.count }}
  </button>
</template>

响应式数据处理

组合式 API 示例 使用 refreactive 管理状态:

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

const count = ref(0);
const user = reactive({
  name: 'Alice',
  age: 25
});

function increaseAge() {
  user.age++;
}
</script>

生命周期钩子使用

常用生命周期

<script setup>
import { onMounted, onUnmounted } from 'vue';

onMounted(() => {
  console.log('组件挂载完成');
});

onUnmounted(() => {
  console.log('组件卸载');
});
</script>

组件间通信

Props 传递数据 父组件传递:

<ChildComponent :message="parentMessage" />

子组件接收:

<script setup>
defineProps({
  message: String
});
</script>

事件发射 子组件触发:

<button @click="$emit('notify')">通知</button>

父组件监听:

<ChildComponent @notify="handleNotify" />

样式与作用域

Scoped CSS 限定样式作用域:

<style scoped>
.button {
  /* 仅影响当前组件 */
}
</style>

CSS 模块化 通过模块名引用:

vue页面实现

<template>
  <div :class="$style.redText">文本</div>
</template>

<style module>
.redText {
  color: red;
}
</style>

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

相关文章

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue实现webshell

vue实现webshell

Vue 实现 WebShell 使用 Vue 实现 WebShell 需要结合前端与后端的交互,通常通过 WebSocket 或 HTTP 协议与服务器通信。以下是一个基于 Vue 和 WebSock…

vue实现spa

vue实现spa

Vue 实现 SPA(单页应用)的步骤 创建 Vue 项目 使用 Vue CLI 快速初始化一个 Vue 项目,确保安装了 Vue Router 作为路由管理工具。运行以下命令创建项目: vue c…