Sample Code for webpack + vuex + axios cross-origin request data, webpackvuex

Source: Internet
Author: User

Sample Code for webpack + vuex + axios cross-origin request data, webpackvuex

This article describes the sample code for cross-origin request data of webpack + vuex + axios:

Use vue-li to build a 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'    }    }  }, }

Cross-origin 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) })}

Use in components:

<template>  <div class="movie-page">    <ul class="clearfix">      <movies-item v-for="(item,index) in movie_list" :key="index" :movie="item"></movies-item>    </ul>  </div></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>

Cross-origin in components

Set in main. js:

Import axios from 'axios '// rewrite axios to the prototype attribute of Vue so that axiosVue. prototype. $ axios = axios can be used in other components.

Set in the component:

<template>  <div class="movie-page">    <ul class="clearfix">      <movies-item v-for="(item,index) in movie_list" :key="index" :movie="item"></movies-item>          </ul>  </div></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>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.