【发布时间】:2019-02-08 12:39:21
【问题描述】:
我尝试将 vue.js 与 Spring Boot 集成。这是我的 vue.js 代码:
<template>
// ...
</template>
<script>
export default {
name: "Login",
data: function() {
return {
username: '',
password: '',
msg: ''
}
},
methods: {
// post data to Spring Boot
login() {
axios.post('/login',{
username: this.username,
password: this.password
})
.then(function(response) {
if(response.data.code === 200){
this.$store.dispatch('setCurrentUser',this.username);
// vue-route
this.$router.push('/course');
} else {
this.msg = response.message;
}
})
.catch(function(err) {
this.msg = 'error';
});
}
}
};
</script>
这是我的 Spring Boot 控制器:
@RestController
@ResponseBody
public class LoginController {
@Autowired
private ResultGenerator resultGenerator;
@PostMapping("/login")
public RestResult login(String username, String password){
if(username.equals("123") && password.equals("123")){
return resultGenerator.getSuccessResult();
} else {
return resultGenerator.getFailResult("error");
}
}
}
控制器将返回类似于:{"code":200,"message":"success","data":null} 的 JSON 数据。当login 方法被调用时,控制器可以接受用户名和密码并且控制器也发送响应数据。但这就是全部,vue-router 没有工作。我在浏览器中看到的只是:
谁能帮忙?
------------------ 加法 -----------------------
这是 vue-router 配置:
const routes = [
{
path: '/',
component: Login
},
{
path: '/signin',
component: Signin
},
{
path: '/course',
component: Course
}
];
const router = new VueRouter({
routes,
mode: "history"
});
【问题讨论】:
标签: spring-boot vue.js