vue+tp5實(shí)現(xiàn)簡(jiǎn)單登錄功能
本文實(shí)例為大家分享了vue+tp5實(shí)現(xiàn)簡(jiǎn)單登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
準(zhǔn)備工作:安裝vue-cli,element-ui,package.json中如圖所示,看著安裝吧

1.在src目錄下新建一個(gè)views放置頁(yè)面

2. 在/src/router/index.js中寫(xiě)入:
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中開(kāi)始寫(xiě)代碼(找一個(gè)登陸模板):
<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="用戶(hù)名"></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: '錯(cuò)誤',
message: '用戶(hù)名不能為空'
});
return false
} else if (!password) {
this.$notify.error({
title: '錯(cuò)誤',
message: '密碼不能為空'
})
return false
} else {
// 將信息傳到后臺(tái)進(jìn)行處理
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進(jìn)行跨域請(qǐng)求
proxyTable: {
'/api/*': { // api為代理接口
target: 'http://localhost:8085/index.php/index', // 這里我代理到本地服務(wù)
changeOrigin: true,
pathRewrite: {
// 這里是追加鏈接,比如真是接口里包含了 /api,就需要這樣配置.
'^/api': '/', // 和下邊兩種寫(xiě)法,因人而異根據(jù)需求。
// 等價(jià)于 /api + /api == http://localhost:54321/api
// 如果寫(xiě)為 '^/api' : '/'
// 等價(jià)于 /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后臺(tái)中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不起作用,驗(yàn)證該接口是否正確,然后在cmd中找到項(xiàng)目目錄然后運(yùn)行:npm run dev
效果:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)未登錄跳轉(zhuǎn)到登錄頁(yè)面的方法
- Vue學(xué)習(xí)之路之登錄注冊(cè)實(shí)例代碼
- vue 微信授權(quán)登錄解決方案
- vue+springboot前后端分離實(shí)現(xiàn)單點(diǎn)登錄跨域問(wèn)題解決方法
- vue實(shí)現(xiàn)登錄后頁(yè)面跳轉(zhuǎn)到之前頁(yè)面
- 使用Vue.js和Element-UI做一個(gè)簡(jiǎn)單登錄頁(yè)面的實(shí)例
- vue路由跳轉(zhuǎn)時(shí)判斷用戶(hù)是否登錄功能的實(shí)現(xiàn)
- Vue 實(shí)現(xiàn)登錄界面驗(yàn)證碼功能
- vue實(shí)現(xiàn)登錄頁(yè)面的驗(yàn)證碼以及驗(yàn)證過(guò)程解析(面向新手)
- vue.js實(shí)現(xiàn)用戶(hù)評(píng)論、登錄、注冊(cè)、及修改信息功能
相關(guān)文章
vue路由$router.push()使用query傳參的實(shí)際開(kāi)發(fā)使用
在vue項(xiàng)目中我們用函數(shù)式編程this.$router.push跳轉(zhuǎn),用query傳遞一個(gè)對(duì)象時(shí)要把這個(gè)對(duì)象先轉(zhuǎn)化為字符串,然后在接收的時(shí)候要轉(zhuǎn)化為對(duì)象,下面這篇文章主要給大家介紹了關(guān)于vue路由$router.push()使用query傳參的實(shí)際開(kāi)發(fā)使用,需要的朋友可以參考下2022-11-11
Vue項(xiàng)目打包后可修改基礎(chǔ)接口地址配置的具體操作
vue項(xiàng)目打包過(guò)后發(fā)現(xiàn)地址錯(cuò)了或者發(fā)布到別的服務(wù)器發(fā)現(xiàn)請(qǐng)求的地址不對(duì),每次都要去重新打包,太浪費(fèi)時(shí)間,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目打包后可修改基礎(chǔ)接口地址配置的具體操作,需要的朋友可以參考下2022-08-08
使用Vue和ECharts創(chuàng)建交互式圖表的代碼示例
在現(xiàn)代 Web 應(yīng)用中,數(shù)據(jù)可視化是一個(gè)重要的組成部分,它不僅能夠幫助用戶(hù)更好地理解復(fù)雜的數(shù)據(jù),還能提升用戶(hù)體驗(yàn),本文給大家使用Vue和ECharts創(chuàng)建交互式圖表的示例,需要的朋友可以參考下2024-11-11
vue中如何實(shí)現(xiàn)后臺(tái)管理系統(tǒng)的權(quán)限控制的方法步驟
這篇文章主要介紹了vue中如何實(shí)現(xiàn)后臺(tái)管理系統(tǒng)的權(quán)限控制的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
vue3.0運(yùn)行npm run dev報(bào)錯(cuò)Cannot find module&
本文主要介紹了vue3.0運(yùn)行npm run dev報(bào)錯(cuò)Cannot find module node:url,因?yàn)槭褂玫膎ode版本是14.15.1低于15.0.0導(dǎo)致,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10

