vue制作一个后台登陆界面

下面是一个简单的示例,演示了如何使用Vue.js来创建一个基本的后台登录界面博客。

首先,确保你已经安装了Vue CLI,然后创建一个新的Vue项目:

vue create my-blog-app

然后,进入项目目录并安装一些需要的依赖:

bash

cd my-blog-app
npm install axios vue-router

创建一个登录页面组件 Login.vue:

<template>
  <div>
    <h2>后台登录</h2>
    <form @submit.prevent="login">
      <div>
        <label for="username">用户名:</label>
        <input type="text" id="username" v-model="username" required>
      </div>
      <div>
        <label for="password">密码:</label>
        <input type="password" id="password" v-model="password" required>
      </div>
      <button type="submit">登录</button>
    </form>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      username: '',
      password: ''
    };
  },
  methods: {
    login() {
      axios.post('/api/login', { username: this.username, password: this.password })
        .then(response => {
          if (response.data.success) {
            // 登录成功,跳转到博客管理页面
            this.$router.push('/admin/blog');
          } else {
            // 登录失败,显示错误信息
            alert('登录失败,请检查用户名和密码');
          }
        })
        .catch(error => {
          console.error('登录失败:', error);
          alert('登录失败,请稍后重试');
        });
    }
  }
};
</script>

创建一个博客管理页面组件 AdminBlog.vue:

<template>
  <div>
    <h2>博客管理</h2>
    <!-- 博客列表 -->
    <ul>
      <li v-for="(post, index) in posts" :key="index">
        <h3>{{ post.title }}</h3>
        <p>{{ post.content }}</p>
      </li>
    </ul>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      posts: []
    };
  },
  created() {
    // 在页面加载时获取博客列表
    this.fetchPosts();
  },
  methods: {
    fetchPosts() {
      axios.get('/api/posts')
        .then(response => {
          this.posts = response.data;
        })
        .catch(error => {
          console.error('获取博客列表失败:', error);
          alert('获取博客列表失败,请稍后重试');
        });
    }
  }
};
</script>

然后,配置路由,在 src/router/index.js 中添加:

import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '../views/Login.vue';
import AdminBlog from '../views/AdminBlog.vue';

Vue.use(VueRouter);

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/admin/blog',
    name: 'AdminBlog',
    component: AdminBlog,
    meta: { requiresAuth: true } // 需要登录才能访问
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

// 添加路由守卫,检查用户是否登录
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 这个路由需要登录权限
    if (!localStorage.getItem('loggedIn')) {
      // 未登录,跳转到登录页面
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      });
    } else {
      // 已登录,放行
      next();
    }
  } else {
    // 不需要登录权限,直接放行
    next();
  }
});

export default router;

接下来,配置一些假的后端接口,假设登录接口在 /api/login,获取博客列表的接口在 /api/posts。在开发环境中,可以使用
json-server 来模拟这些接口:

首先,安装 json-server:

npm install -g json-server

然后,在项目根目录创建一个 db.json 文件:

{
  "users": [
    { "id": 1, "username": "admin", "password": "admin" }
  ],
  "posts": [
    { "id": 1, "title": "第一篇博客", "content": "这是我的第一篇博客内容。" },
    { "id": 2, "title": "第二篇博客", "content": "这是我的第二篇博客内容。" }
  ]
}

最后,在命令行中启动 json-server:

json-server --watch db.json --port 3001

现在,你可以通过 http://localhost:3001 访问模拟的后端接口了。
最后,在 src/main.js 中,添加 Axios 的配置:

import Vue from 'vue';
import App from './App.vue';
import router from './router';
import axios from 'axios';

Vue.config.productionTip = false;

// 配置axios基本URL
axios.defaults.baseURL = 'http://localhost:3001';

Vue.prototype.$axios = axios;

new Vue({
  router,
  render: h => h(App)
}).$mount('#app');

现在你可以启动开发服务器了:

bash

npm run serve

访问 http://localhost:8080,你将看到登录页面。登录成功后,将会跳转到博客管理页面,显示博客列表。

这只是一个简单的示例,实际的应用可能会更加复杂,比如添加用户注册、权限管理、博客编辑等功能。

  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element UI 和 Vue.js 是一对强力的组合,可以帮助我们轻松地创建漂亮的前端界面。下面是一个简单的后台管理界面的示例,使用 Element UI 和 Vue.js 来构建。 首先,我们需要安装 Element UI 和 Vue.js。可以通过以下命令来安装: ``` npm install element-ui vue --save ``` 接下来,我们需要在 Vue.js 中引入 Element UI,并注册它们的组件。可以在 main.js 文件中添加以下代码: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 这将在 Vue.js 中引入 Element UI,并注册它们的组件。接下来,我们可以创建一个简单的后台管理界面的组件,例如: ```vue <template> <div> <el-menu default-active="1" class="el-menu-vertical-demo" @select="handleSelect"> <el-menu-item index="1"> <i class="el-icon-menu"></i> <span>Dashboard</span> </el-menu-item> <el-menu-item index="2"> <i class="el-icon-document"></i> <span>Articles</span> </el-menu-item> <el-menu-item index="3"> <i class="el-icon-setting"></i> <span>Settings</span> </el-menu-item> </el-menu> <el-main> <h1>{{ pageTitle }}</h1> <p>{{ pageContent }}</p> </el-main> </div> </template> <script> export default { name: 'Dashboard', data () { return { pageTitle: 'Dashboard', pageContent: 'Welcome to your dashboard!' } }, methods: { handleSelect (index) { switch (index) { case '1': this.pageTitle = 'Dashboard' this.pageContent = 'Welcome to your dashboard!' break case '2': this.pageTitle = 'Articles' this.pageContent = 'Here are all your articles.' break case '3': this.pageTitle = 'Settings' this.pageContent = 'Here you can configure your settings.' break } } } } </script> ``` 这个组件包括一个垂直菜单和一个主要内容区域。当用户选择菜单项时,页面的标题和内容会相应地更新。现在我们可以在我们的应用程序中使用这个组件: ```vue <template> <div id="app"> <Dashboard /> </div> </template> <script> import Dashboard from './components/Dashboard.vue' export default { components: { Dashboard } } </script> ``` 这将在我们的应用程序中展示这个后台管理界面的组件。 当然,这只是一个简单的示例。通过 Element UI 和 Vue.js,我们可以轻松地创建更复杂和更漂亮的后台管理界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值