Home  >  Article  >  Web Front-end  >  Use webpack+vuex+axios technologies to implement cross-domain request data (detailed tutorial)

Use webpack+vuex+axios technologies to implement cross-domain request data (detailed tutorial)

亚连
亚连Original
2018-06-01 09:50:261930browse

This article mainly introduces webpack vuex axios cross-domain request data. Now I share it with you and give it as a reference.

This article introduces the sample code of webpack vuex axios cross-domain request data and shares it with everyone. The details are as follows:

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

I want to make a cross-domain request 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)
 })
}

Use 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 {mapState, mapActions, mapGetters} from &#39;vuex&#39;;
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(&#39;GET_IN_THEATERS&#39;)
  },
  mounted () {
  }
}
</script>
<style lang="scss">
@import "./../../assets/reset.scss";
@import "./../../assets/main.scss";
.movie-page{
  padding: 0 rem(40);
}
</style>

I want to make a cross-domain request within the component Domain

is set in main.js:

import axios from &#39;axios&#39;
// 将 axios 改写为 Vue 的原型属性,使在其它的组件中可以使用 axios
Vue.prototype.$axios = axios

is set in 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(&#39;/v2/movie/in_theaters&#39;).then(res => {
      this.movie_list = res.data.subjects
    }, res => {
      console.infor(&#39;error&#39;)
    })
  }
}
</script>
<style lang="scss">
@import "./../../assets/reset.scss";
@import "./../../assets/main.scss";
.movie-page{
  padding: 0 rem(40);
}
</style>

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Vue.js realizes the random dragging method of images

Solution to Vue Modify the array through the following table , the problem of page not rendering

vue2.0 axios cross-domain and rendering problem solution

The above is the detailed content of Use webpack+vuex+axios technologies to implement cross-domain request data (detailed tutorial). 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