詳解前后端分離之VueJS前端
前言
前端用什么框架都可以,這里選擇小巧的vuejs。
要實(shí)現(xiàn)的功能很簡單:
1、登錄功能,成功將服務(wù)器返回的token存在本地
2、使用帶token的header訪問服務(wù)器的一個(gè)資源
本次實(shí)驗(yàn)環(huán)境:
"dependencies": { "vue": "^2.2.1" }, "devDependencies": { "babel-core": "^6.0.0", "babel-loader": "^6.0.0", "babel-preset-latest": "^6.0.0", "cross-env": "^3.0.0", "css-loader": "^0.25.0", "file-loader": "^0.9.0", "vue-loader": "^11.1.4", "vue-template-compiler": "^2.2.1", "webpack": "^2.2.0", "webpack-dev-server": "^2.2.0" }
開發(fā)IDE:Atom
首先建一個(gè)項(xiàng)目
使用webpack構(gòu)建
/Atom# vue init webpack-simple vue-jwt-demo ... /Atom# cd vue-jwt-demo/ /Atom/vue-jwt-demo# cnpm install /Atom/vue-jwt-demo# npm run dev
安裝插件
/Atom/vue-jwt-demo# cnpm install vue-router /Atom/vue-jwt-demo# cnpm install vue-resource
整體目錄
auth.js
完成token的存取
const SERVER_URL = 'http://localhost:8081' const LOGIN_URL = SERVER_URL+'/login2' export default{ data:{ authenticated:false }, login(context,info){ context.$http.post(LOGIN_URL,info).then(function(data){ console.log(data.bodyText) localStorage.setItem('token',data.bodyText); this.authenticated = true //跳到home頁 this.$router.push('home') },function(err){ console.log(err+","+err.body.message) context.error = err.body.message }) }, getAuthHeader(){ return { 'Authorization':'Bearer '+localStorage.getItem('token') } }, checkAuth(){ var token = localStorage.getItem('token') if(token){ this.authenticated = true }else{ this.authenticated = false } } }
main.js
程序入口:完成路由和初始化
import Vue from 'vue' import App from './App.vue' import Login from './component/Login.vue' import Home from './component/Home.vue' import VueRouter from 'vue-router' import VueResource from 'vue-resource' import auth from './auth/auth' Vue.use(VueRouter) Vue.use(VueResource) //在啟動(dòng)APP時(shí)進(jìn)行校驗(yàn)是否有token auth.checkAuth() const routes= [ { path:'/',redirect:'/login' }, { path:'/login',component:Login }, { path:'/home',component:Home } ] const router = new VueRouter({ routes }) new Vue({ router, render: h => h(App) }).$mount('#app')
App.vue
頁面載體
<template> <div id="app"> <h1>{{msg}}</h1> <router-view></router-view> </div> </template> <script> export default { name: 'app', data () { return { msg: 'Vue前后端分離demo' } } } </script>
Login.vue
登錄頁面
<template> <div> <h2>登錄</h2> <p>{{ error }}</p> <div> <input type="text" placeholder="Enter your username" v-model="info.username" > </div> <div> <input type="password" placeholder="Enter your password" v-model="info.password" > </div> <button @click="submit()">登錄</button> </div> </template> <script> import auth from '../auth/auth' export default { data() { return { info: { username: '', password: '' }, error: '' } }, methods: { submit() { var info = { username: this.info.username, password: this.info.password } auth.login(this, info) } } } </script>
效果:丑是丑了點(diǎn)
Home.vue
主頁面,訪問一個(gè)獲取郵箱的請求
<template> <div id="home"> <h1>{{msg}}</h1> <button @click="getEmail()">Get Email</button> <h2>Email:{{email}}</h2> </div> </template> <script> import auth from '../auth/auth' export default { name: 'home', data () { return { msg: '歡迎您登錄成功', email:'' } }, beforeCreate(){ //如果沒有token的話需要重新登錄 if(!auth.authenticated){ this.$router.push('login') } }, methods:{ getEmail(){ this.$http.get('http://localhost:8081/user/getEmail',{ headers:auth.getAuthHeader() }).then(function(re){ this.email = re.bodyText },function(){ console.log("get email error") }) } } } </script> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } h1, h2 { font-weight: normal; } a { color: #42b983; } </style>
對應(yīng)在服務(wù)端:
@GetMapping("/getEmail") public String getEmail() { return "xxxx@qq.com"; }
可看到瀏覽器的本地存儲(chǔ):
代碼:https://github.com/jimolonely/vue-jwt-demo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue2實(shí)時(shí)監(jiān)聽表單變化的示例講解
今天小編就為大家分享一篇Vue2實(shí)時(shí)監(jiān)聽表單變化的示例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08使用Vue.js創(chuàng)建一個(gè)時(shí)間跟蹤的單頁應(yīng)用
這篇文章主要介紹了使用Vue.js創(chuàng)建一個(gè)時(shí)間跟蹤的單頁應(yīng)用的相關(guān)資料,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11antd+vue實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證循環(huán)屬性表單的思路
今天通過本文給大家分享antd+vue實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證循環(huán)屬性表單的思路,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-09-09vue-router清除url地址欄路由參數(shù)的操作代碼
這篇文章主要介紹了vue-router清除url地址欄路由參數(shù),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2015-11-11如何解決element-ui動(dòng)態(tài)加載級聯(lián)選擇器默認(rèn)選中問題
這篇文章主要介紹了如何解決element-ui動(dòng)態(tài)加載級聯(lián)選擇器默認(rèn)選中問題,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09vue路由跳轉(zhuǎn)打開新窗口(window.open())和關(guān)閉窗口(window.close())
這篇文章主要介紹了vue路由跳轉(zhuǎn)打開新窗口(window.open())和關(guān)閉窗口(window.close())問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue使用動(dòng)態(tài)添加路由(router.addRoutes)加載權(quán)限側(cè)邊欄的方式
這篇文章主要介紹了vue使用動(dòng)態(tài)添加路由(router.addRoutes)加載權(quán)限側(cè)邊欄的方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06npm?install安裝報(bào)錯(cuò)的幾種常見情況
當(dāng)你跑起一個(gè)項(xiàng)目的時(shí)候,第一步需要先安裝依賴npm install,下面這篇文章主要給大家介紹了關(guān)于npm?install安裝報(bào)錯(cuò)的幾種常見情況,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07