• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >vue > 详解前后端分离之VueJS前端

详解前后端分离之VueJS前端

作者:root__1024 字体:[增加 减小] 来源:互联网 时间:2017-05-30

本文主要包含vuejs,vuejs官网,vuejs教程,vuejs视频教程,vuejs下载等相关知识,root__1024 希望在学习及工作中可以帮助到您

前言

前端用什么框架都可以,这里选择小巧的vuejs。

要实现的功能很简单:

1、登录功能,成功将服务器返回的token存在本地

2、使用带token的header访问服务器的一个资源

本次实验环境:

"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"
 }

</div>

开发IDE:Atom

首先建一个项目

使用webpack构建

/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
</div>

安装插件

/Atom/vue-jwt-demo# cnpm install vue-router

/Atom/vue-jwt-demo# cnpm install vue-resource
</div>

整体目录

1

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
    }
  }
}

</div>

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)

//在启动APP时进行校验是否有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')
</div>

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>
</div>

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>
</div>

效果:丑是丑了点

2

Home.vue

主页面,访问一个获取邮箱的请求

<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>

</div>

对应在服务端:

@GetMapping("/getEmail")
  public String getEmail() {
    return "xxxx@qq.com";
  }
</div>

3

可看到浏览器的本地存储:

4

代码:https://github.com/jimolonely/vue-jwt-demo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

</div>

您可能想查找下面的文章:

  • vuejs怎么在服务器上发布部署
  • 详解前后端分离之VueJS前端
  • 详解VueJs前后端分离跨域问题
  • vuejs2.0子组件改变父组件的数据实例
  • Vuejs实现带样式的单文件组件新方法
  • vuejs如何配置less
  • VueJS如何引入css或者less文件的一些坑
  • 一篇看懂vuejs的状态管理神器 vuex状态管理模式
  • 详解Vuejs2.0之异步跨域请求
  • Vuejs入门教程之Vue生命周期,数据,手动挂载,指令,过滤器

相关文章

  • 2017-05-30vue2.0数据双向绑定与表单bootstrap+vue组件
  • 2017-05-30vue.js实现含搜索的多种复选框(附源码)
  • 2017-05-30Vue 2.0中生命周期与钩子函数的一些理解
  • 2017-05-30Vue过滤器的用法和自定义过滤器使用
  • 2017-05-30Vue计算属性的学习笔记
  • 2017-05-30VUE实现日历组件功能
  • 2017-05-30简单的vue-resourse获取json并应用到模板示例
  • 2017-05-30基于Vue.js的表格分页组件
  • 2017-05-30深入浅析Vue组件开发
  • 2017-05-30Vue input控件通过value绑定动态属性及修饰符的方法

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • Vue2组件tree实现无限级树形菜单
    • vue实现可增删查改的成绩单
    • Vue之嵌套router传参params与query
    • VueJs路由跳转——vue-router的使用详解
    • 详解Vue 动态添加模板的几种方法
    • Vue监听数组变化源码解析
    • vue 和vue-touch 实现移动端左右导航效果(仿京东移动站导航)
    • 利用vue写todolist单页应用
    • Vue.js实现文章评论和回复评论功能
    • require.js+vue开发微信上传图片组件

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有