vue+tp5實現(xiàn)簡單登錄功能
本文實例為大家分享了vue+tp5實現(xiàn)簡單登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
準備工作:安裝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中恢復(fù)成如下圖:

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中設(shè)置proxytable,利用axios進行跨域請求
proxyTable: {
'/api/*': { // api為代理接口
target: 'http://localhost:8085/index.php/index', // 這里我代理到本地服務(wù)
changeOrigin: true,
pathRewrite: {
// 這里是追加鏈接,比如真是接口里包含了 /api,就需要這樣配置.
'^/api': '/', // 和下邊兩種寫法,因人而異根據(jù)需求。
// 等價于 /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]);
}
}
注意:如果設(shè)置的proxytable不起作用,驗證該接口是否正確,然后在cmd中找到項目目錄然后運行:npm run dev
效果:


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue路由$router.push()使用query傳參的實際開發(fā)使用
在vue項目中我們用函數(shù)式編程this.$router.push跳轉(zhuǎn),用query傳遞一個對象時要把這個對象先轉(zhuǎn)化為字符串,然后在接收的時候要轉(zhuǎn)化為對象,下面這篇文章主要給大家介紹了關(guān)于vue路由$router.push()使用query傳參的實際開發(fā)使用,需要的朋友可以參考下2022-11-11
使用Vue和ECharts創(chuàng)建交互式圖表的代碼示例
在現(xiàn)代 Web 應(yīng)用中,數(shù)據(jù)可視化是一個重要的組成部分,它不僅能夠幫助用戶更好地理解復(fù)雜的數(shù)據(jù),還能提升用戶體驗,本文給大家使用Vue和ECharts創(chuàng)建交互式圖表的示例,需要的朋友可以參考下2024-11-11
vue中如何實現(xiàn)后臺管理系統(tǒng)的權(quán)限控制的方法步驟
這篇文章主要介紹了vue中如何實現(xiàn)后臺管理系統(tǒng)的權(quán)限控制的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
vue3.0運行npm run dev報錯Cannot find module&
本文主要介紹了vue3.0運行npm run dev報錯Cannot find module node:url,因為使用的node版本是14.15.1低于15.0.0導致,具有一定的參考價值,感興趣的可以了解一下2023-10-10

