当前位置:   article > 正文

vue实现axios和事件Bus等父子组件的事件传递实现

vue实现axios和事件Bus等父子组件的事件传递实现

发送请求的配置

vue.config.js

  1. const { defineConfig } = require('@vue/cli-service')
  2. module.exports = defineConfig({
  3. transpileDependencies: true,
  4. devServer: {
  5. proxy:{
  6. '/api':{
  7. target:'http://localhost:7001',
  8. changeOrigin:true,
  9. pathRewrite:{
  10. '^/api':'' //重写api,把api变成空字符,因为我们真正请求的路径是没有api的
  11. }
  12. }
  13. }
  14. }
  15. })

  1. import axios from "axios";
  2. const service = axios.create({
  3. timeout: 5000,
  4. baseURL: 'api',
  5. withCredentials: true
  6. });
  7. // request interceptor
  8. service.interceptors.request.use(
  9. config => {
  10. const token= localStorage.getItem("token")
  11. // do something before request is sent
  12. if (localStorage.getItem("token") == undefined) {
  13. // config.headers['Authorization'] = 'Basic ' + Base64.encode('ha_web:ha_web_secret')
  14. } else {
  15. // config.headers['Authorization'] = 'Basic ' + Base64.encode('ha_web:ha_web_secret')
  16. // config.headers['cloud-auth'] = 'bearer ' + token
  17. config.headers['token'] = token
  18. }
  19. // config.headers['User-Type'] = 'pos'
  20. // if (store.getters.token) {
  21. // // let each request carry token
  22. // // ['X-Token'] is a custom headers key
  23. // // please modify it according to the actual situation
  24. // config.headers['X-Token'] = getToken()
  25. // }
  26. return config
  27. },
  28. error => {
  29. // do something with request error
  30. return Promise.reject(error)
  31. }
  32. )
  33. service.interceptors.response.use(
  34. /**
  35. * If you want to get http information such as headers or status
  36. * Please return response => response
  37. */
  38. /**
  39. * Determine the request status by custom code
  40. * Here is just an example
  41. * You can also judge the status by HTTP Status Code
  42. */
  43. response => {
  44. const res = response.data
  45. // if the custom code is not 20000, it is judged as an error.
  46. // if (res.code == 200 || res.code == 400) {
  47. // return res
  48. // } else {
  49. // Message({
  50. // message: res.msg || 'Error',
  51. // type: 'error',
  52. // duration: 5 * 1000
  53. // })
  54. // if (res.code === 401) {
  55. // store.dispatch('user/resetToken').then(() => {
  56. // location.reload()
  57. // })
  58. // }
  59. // return Promise.reject(new Error(res.msg || 'Error'))
  60. // }
  61. return res
  62. },
  63. error => {
  64. // if (error.response.data.code == 401) {
  65. // MessageBox.error('请求为授权,请重新登陆')
  66. // store.dispatch('user/resetToken').then(() => {
  67. // location.reload()
  68. // })
  69. // } else if (error.response.data.code == 400) {
  70. // Message.error(error.response.data.msg)
  71. // } else {
  72. // Message.error('未知异常')
  73. // }
  74. return Promise.reject(error)
  75. }
  76. )
  77. export default service;

vue中bus的事件线传递接收

  1. this.$eventBus.$on('show-login-dialog', () => {
  2. // 在这里编写弹出 el-dialog 的逻辑
  3. // 例.centerDialogVisible
  4. this.centerDialogVisible = true;
  5. })

路由守卫中,使用bus事件传递信息,弹出事件

  1. router.beforeEach((to,from,next)=>{
  2. // if(localStorage.getItem('token')){
  3. // next('/');
  4. // }
  5. if(to.path ==='/login' || to.path ==='/'){
  6. next()
  7. }else{
  8. if(localStorage.getItem('token')){
  9. next()
  10. }else{
  11. // 跳转到 path: '/', name: 'home',
  12. next('/');
  13. //使用事件bus
  14. Vue.prototype.$eventBus.$emit('show-login-dialog')
  15. // next('/login')
  16. }
  17. }
  18. })

父子组件之间的事件传递接收

  1. setSearchParma() {
  2. this.$emit('transfer', this.searchParams.searchName) //触发transfer方法
  3. }

接收组件的信息

  <AppHeader  @transfer="getSearchParam"></AppHeader>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/359237
推荐阅读
相关标签
  

闽ICP备14008679号