当前位置:首页 > VUE

vue实现的页面

2026-01-08 12:54:23VUE

Vue 实现的页面示例

Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是一个简单的 Vue 页面实现示例,包含基本的结构和功能。

安装 Vue

通过 CDN 引入 Vue:

<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>

或通过 npm 安装:

npm install vue

基本页面结构

一个 Vue 页面通常包含以下部分:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue Page</title>
</head>
<body>
  <div id="app">
    {{ message }}
  </div>

  <script>
    const { createApp } = Vue;

    createApp({
      data() {
        return {
          message: 'Hello Vue!'
        };
      }
    }).mount('#app');
  </script>
</body>
</html>

数据绑定

Vue 使用双大括号语法进行文本插值:

<div id="app">
  <p>{{ message }}</p>
</div>

指令

Vue 提供了多种指令,如 v-ifv-forv-on 等:

<div id="app">
  <p v-if="showMessage">{{ message }}</p>
  <button v-on:click="toggleMessage">Toggle</button>
</div>

<script>
  createApp({
    data() {
      return {
        message: 'Hello Vue!',
        showMessage: true
      };
    },
    methods: {
      toggleMessage() {
        this.showMessage = !this.showMessage;
      }
    }
  }).mount('#app');
</script>

组件化

Vue 支持组件化开发,可以创建可复用的组件:

<div id="app">
  <my-component></my-component>
</div>

<script>
  const { createApp } = Vue;

  const MyComponent = {
    template: `<p>This is a component</p>`
  };

  createApp({
    components: {
      MyComponent
    }
  }).mount('#app');
</script>

路由

使用 Vue Router 实现页面路由:

npm install vue-router@4
<div id="app">
  <router-view></router-view>
</div>

<script>
  const { createApp } = Vue;
  const { createRouter, createWebHistory } = VueRouter;

  const Home = { template: '<div>Home</div>' };
  const About = { template: '<div>About</div>' };

  const router = createRouter({
    history: createWebHistory(),
    routes: [
      { path: '/', component: Home },
      { path: '/about', component: About }
    ]
  });

  createApp({}).use(router).mount('#app');
</script>

状态管理

使用 Vuex 进行状态管理:

npm install vuex@next
<div id="app">
  <p>{{ count }}</p>
  <button @click="increment">Increment</button>
</div>

<script>
  const { createApp } = Vue;
  const { createStore } = Vuex;

  const store = createStore({
    state() {
      return {
        count: 0
      };
    },
    mutations: {
      increment(state) {
        state.count++;
      }
    }
  });

  const app = createApp({
    computed: {
      count() {
        return store.state.count;
      }
    },
    methods: {
      increment() {
        store.commit('increment');
      }
    }
  });

  app.use(store).mount('#app');
</script>

生命周期钩子

Vue 组件有多个生命周期钩子,如 createdmounted

<div id="app">
  <p>{{ message }}</p>
</div>

<script>
  createApp({
    data() {
      return {
        message: ''
      };
    },
    created() {
      this.message = 'Component created';
    },
    mounted() {
      this.message = 'Component mounted';
    }
  }).mount('#app');
</script>

样式绑定

Vue 支持动态绑定样式和类:

<div id="app">
  <p :class="{ active: isActive }">Styled text</p>
  <p :style="{ color: textColor }">Colored text</p>
</div>

<script>
  createApp({
    data() {
      return {
        isActive: true,
        textColor: 'red'
      };
    }
  }).mount('#app');
</script>

表单输入绑定

使用 v-model 实现表单双向绑定:

vue实现的页面

<div id="app">
  <input v-model="message" placeholder="Edit me">
  <p>Message is: {{ message }}</p>
</div>

<script>
  createApp({
    data() {
      return {
        message: ''
      };
    }
  }).mount('#app');
</script>

以上示例展示了 Vue 页面的基本实现方式,可以根据需求扩展和组合这些功能。

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

相关文章

vue项目实现

vue项目实现

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 安装 Vue CLI(脚手架工具): np…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue实现Pop

vue实现Pop

Vue 实现 Popover 组件的方法 使用 Vue 内置指令 v-show/v-if 和事件监听 通过 Vue 的指令和事件绑定实现基础的 Popover 功能。定义一个布尔值控制 Popover…

vue 实现vshow

vue 实现vshow

Vue 实现 v-show 在 Vue 中,v-show 是一个常用的指令,用于根据条件控制元素的显示或隐藏。与 v-if 不同,v-show 不会移除 DOM 元素,而是通过 CSS 的 displ…

vue原生实现

vue原生实现

Vue 原生实现方法 在 Vue 中实现原生功能通常指直接使用 Vue 的核心特性(如指令、响应式数据、组件等)而非依赖第三方库。以下是几种常见场景的原生实现方式: 数据绑定与响应式更新 Vue 的…

vue实现word文档实现预览

vue实现word文档实现预览

在Vue中实现Word文档预览可以通过多种方式实现,以下是几种常见的方法: 使用mammoth.js库 mammoth.js是一个将Word文档(.docx)转换为HTML的JavaScript库,…