当前位置:首页 > VUE

html嵌入vue实现登录

2026-01-21 20:19:50VUE

在HTML中嵌入Vue实现登录功能

引入Vue.js

通过CDN或本地文件引入Vue.js,推荐使用最新稳定版本:

html嵌入vue实现登录

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<!-- 或生产环境版本 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>

创建HTML结构

设计登录表单的基本HTML结构,包含用户名、密码输入框和提交按钮:

html嵌入vue实现登录

<div id="app">
  <form @submit.prevent="handleSubmit">
    <input type="text" v-model="username" placeholder="用户名">
    <input type="password" v-model="password" placeholder="密码">
    <button type="submit">登录</button>
  </form>
  <p v-if="errorMessage" class="error">{{ errorMessage }}</p>
</div>

编写Vue实例

初始化Vue实例并实现登录逻辑:

new Vue({
  el: '#app',
  data: {
    username: '',
    password: '',
    errorMessage: ''
  },
  methods: {
    handleSubmit() {
      if (!this.username || !this.password) {
        this.errorMessage = '用户名和密码不能为空';
        return;
      }

      // 模拟API请求
      setTimeout(() => {
        if (this.username === 'admin' && this.password === '123456') {
          alert('登录成功');
          // 实际项目中这里跳转到首页
        } else {
          this.errorMessage = '用户名或密码错误';
        }
      }, 500);
    }
  }
});

添加样式(可选)

为登录表单添加基础样式:

#app {
  max-width: 300px;
  margin: 0 auto;
  padding: 20px;
}
input {
  display: block;
  width: 100%;
  margin-bottom: 10px;
  padding: 8px;
}
button {
  width: 100%;
  padding: 10px;
  background-color: #42b983;
  color: white;
  border: none;
}
.error {
  color: red;
  margin-top: 10px;
}

完整示例代码

<!DOCTYPE html>
<html>
<head>
  <title>Vue登录示例</title>
  <style>
    #app {
      max-width: 300px;
      margin: 0 auto;
      padding: 20px;
    }
    input {
      display: block;
      width: 100%;
      margin-bottom: 10px;
      padding: 8px;
    }
    button {
      width: 100%;
      padding: 10px;
      background-color: #42b983;
      color: white;
      border: none;
    }
    .error {
      color: red;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <div id="app">
    <form @submit.prevent="handleSubmit">
      <input type="text" v-model="username" placeholder="用户名">
      <input type="password" v-model="password" placeholder="密码">
      <button type="submit">登录</button>
    </form>
    <p v-if="errorMessage" class="error">{{ errorMessage }}</p>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  <script>
    new Vue({
      el: '#app',
      data: {
        username: '',
        password: '',
        errorMessage: ''
      },
      methods: {
        handleSubmit() {
          if (!this.username || !this.password) {
            this.errorMessage = '用户名和密码不能为空';
            return;
          }

          setTimeout(() => {
            if (this.username === 'admin' && this.password === '123456') {
              alert('登录成功');
            } else {
              this.errorMessage = '用户名或密码错误';
            }
          }, 500);
        }
      }
    });
  </script>
</body>
</html>

实际项目扩展建议

  • 使用axios处理真实API请求
  • 添加表单验证库如vee-validate
  • 实现路由跳转功能
  • 添加加载状态指示器
  • 使用Vuex管理用户状态

标签: htmlvue
分享给朋友:

相关文章

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录组…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现导入

vue实现导入

Vue 实现文件导入功能 使用 <input type="file"> 元素 在 Vue 模板中添加一个文件输入元素,绑定 change 事件处理函数。 <template>…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…

vue 实现音乐

vue 实现音乐

Vue 实现音乐播放功能 在 Vue 中实现音乐播放功能,可以通过 HTML5 的 <audio> 元素或第三方库如 howler.js 来实现。以下是两种方法的详细步骤: 使用 HTM…