這篇文章給大家分享的是有關(guān)前后端分離之VueJS前端的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
前言
前端用什么框架都可以,這里選擇小巧的vuejs。
要實(shí)現(xiàn)的功能很簡(jiǎn)單:
1、登錄功能,成功將服務(wù)器返回的token存在本地
2、使用帶token的header訪問(wèn)服務(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" }
開(kāi)發(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頁(yè) 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
頁(yè)面載體
<template> <div id="app"> <h2>{{msg}}</h2> <router-view></router-view> </div> </template> <script> export default { name: 'app', data () { return { msg: 'Vue前后端分離demo' } } } </script>
Login.vue
登錄頁(yè)面
<template> <div> <h3>登錄</h3> <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
主頁(yè)面,訪問(wèn)一個(gè)獲取郵箱的請(qǐng)求
<template> <div id="home"> <h2>{{msg}}</h2> <button @click="getEmail()">Get Email</button> <h3>Email:{{email}}</h3> </div> </template> <script> import auth from '../auth/auth' export default { name: 'home', data () { return { msg: '歡迎您登錄成功', email:'' } }, beforeCreate(){ //如果沒(méi)有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; } h2, h3 { font-weight: normal; } a { color: #42b983; } </style>
對(duì)應(yīng)在服務(wù)端:
@GetMapping("/getEmail") public String getEmail() { return "xxxx@qq.com"; }
可看到瀏覽器的本地存儲(chǔ):
感謝各位的閱讀!關(guān)于“前后端分離之VueJS前端的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前題目:前后端分離之VueJS前端的示例分析-創(chuàng)新互聯(lián)
當(dāng)前地址:http://www.chinadenli.net/article10/dpjpdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、云服務(wù)器、App設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容