vue+tp5實現簡單登錄功能

本文實例為大傢分享瞭vue+tp5實現簡單登錄功能的具體代碼,供大傢參考,具體內容如下

準備工作:安裝vue-cli,element-ui,package.json中如圖所示,看著安裝吧

1.在src目錄下新建一個views放置頁面

2. 在/src/router/index.js中寫入:

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import login from '@/views/login/index.vue'
 
Vue.use(Router)
 
export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    }, {
      path: '/login',
      component: login
    }
  ]
})

3.將app.vue中恢復成如下圖:

4.在/src/views/login/index.vue中開始寫代碼(找一個登陸模板):

<template>
    <div id="app-1">
        <div class="content">
            <div class="content_input">
                <div class="title">
                    <p>管理員登錄</p>
                </div>
                <el-input v-model="UserName" clearable placeholder="用戶名"></el-input>
                <el-input v-model="PassWord" clearable show-password placeholder="密碼"></el-input>
                <div class="content_button">
                    <el-button type="primary" @click="SignIn">登錄</el-button>
                </div>
            </div>
        </div>
    </div>
</template>
 
<script>
import '@/assets/css/style.css'
const axios = require('axios')
export default {
  name: 'Login',
  data () {
    return {
      UserName: '',
      PassWord: ''
    }
  },
  methods: {
    SignIn () {
      let that = this
      let username = that.UserName
      let password = that.PassWord
      if (!username) {
        this.$notify.error({
          title: '錯誤',
          message: '用戶名不能為空'
        });
        return false
      } else if (!password) {
        this.$notify.error({
          title: '錯誤',
          message: '密碼不能為空'
        })
        return false
      } else {
        // 將信息傳到後臺進行處理
        axios.post('/api/login/doLogin', {
          name: username,
          psw: password
        })
          .then(function (response) {
            console.log(response)
          })
          .catch(function (error) {
            console.log(error)
          })
      }
    }
  }
}
</script>

5.在config/index.js中設置proxytable,利用axios進行跨域請求

proxyTable: {
        '/api/*': {    // api為代理接口
            target: 'http://localhost:8085/index.php/index',    // 這裡我代理到本地服務
            changeOrigin: true,
            pathRewrite: {
              // 這裡是追加鏈接,比如真是接口裡包含瞭 /api,就需要這樣配置.
              '^/api': '/', // 和下邊兩種寫法,因人而異根據需求。
              // 等價於    /api + /api  ==  http://localhost:54321/api
              // 如果寫為 '^/api' : '/'
              // 等價於   /api + /  ==  http://localhost:54321/
              // 這裡的 /api ==  http://localhost:54321
            }
        }
    },

6. /src/main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import Router from 'vue-router'
import router from './router'
import axios from 'axios'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.min.css'
 
Vue.use(ElementUI)
Vue.use(Router)
 
Vue.config.productionTip = false
Vue.prototype.$axios = axios
 
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

7.tp5後臺中index/controller/Login.php中:

<?php
/**
 * Created by PhpStorm.
 * User: mx1
 * Date: 2021/11/9
 * Time: 15:21
 */
 
namespace app\index\controller;
 
 
use think\Controller;
 
class Login extends Controller
{
    public function doLogin(){
        $name=input('post.name');
        $psw=input('post.psw');
        return json([$name,$psw]);
    }
 
}

註意:如果設置的proxytable不起作用,驗證該接口是否正確,然後在cmd中找到項目目錄然後運行:npm run dev

效果:

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: