Home  >  Article  >  Web Front-end  >  How webpack+vuex implements cross-domain request data

How webpack+vuex implements cross-domain request data

php中世界最好的语言
php中世界最好的语言Original
2018-04-11 17:11:201746browse

This time I will show you how webpack vuex implements cross-domain request data, and what are the precautions for webpack vuex to implement cross-domain request data. The following is a practical case, let's take a look.

Use vue-li to build the webpack project and modify the bulid/config/index.js file

dev: {
  env: require('./dev.env'),
  port: process.env.PORT || 8080,
  autoOpenBrowser: true,
  assetsSubDirectory: 'static',
  assetsPublicPath: '/',
  proxyTable: {
   '/v2': {
     target: 'http://api.douban.com',
     changeOrigin: true,
     pathRewrite: {
      '^/v2': '/v2'
    } 
   }
  },
 }
Want to make cross-domain requests in action.js

Set action.js:

import axios from 'axios'
export const GET_IN_THEATERS = ({
 dispatch,
 state,
 commit
}) => {
 axios({
  url: '/v2/movie/in_theaters'
 }).then(res => {
  commit('in_theaters', res.data)
 })
}
Used within components:

<template>
  <p class="movie-page">
    <ul class="clearfix">
      <movies-item v-for="(item,index) in movie_list" :key="index" :movie="item"></movies-item>
    </ul>
  </p>
</template>
<script>
import {mapState, mapActions, mapGetters} from 'vuex';
import MoviesItem from "./movie-item";
export default {
  data () {
    return {
      
    }
  },
  components: {
    MoviesItem
  },
  computed: {
    ...mapState({
      movie_list: state => {
        return state.in_theaters.subjects
      }
    })
  },
  methods: {
    
  },
  created () {
    this.$store.dispatch('GET_IN_THEATERS')
  },
  mounted () {
  }
}
</script>
<style lang="scss">
@import "./../../assets/reset.scss";
@import "./../../assets/main.scss";
.movie-page{
  padding: 0 rem(40);
}
</style>
Want to cross domain within the component

Set in main.js:

import axios from 'axios'
// 将 axios 改写为 Vue 的原型属性,使在其它的组件中可以使用 axios
Vue.prototype.$axios = axios
Set within the component:

<template>
  <p class="movie-page">
    <ul class="clearfix">
      <movies-item v-for="(item,index) in movie_list" :key="index" :movie="item"></movies-item>
      
    </ul>
  </p>
</template>
<script>
import MoviesItem from "./movie-item";
export default {
  data () {
    return {
      movie_list: []
    }
  },
  components: {
    MoviesItem
  },
  computed: {
    
  },
  methods: {
  },
  created () {
    
  },
  mounted () {
    this.$axios.get('/v2/movie/in_theaters').then(res => {
      this.movie_list = res.data.subjects
    }, res => {
      console.infor('error')
    })
  }
}
</script>
<style lang="scss">
@import "./../../assets/reset.scss";
@import "./../../assets/main.scss";
.movie-page{
  padding: 0 rem(40);
}
</style>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the steps for vue to use the xe-utils function library

Vue refreshes after packaging the project How to deal with 404 display

The above is the detailed content of How webpack+vuex implements cross-domain request data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn