This article introduces the sample code of webpack+vuex+axios cross-domain request data, and shares it with you, as follows:
Use vue-li to build webpack project and modify the build/config/file
dev: { env: require('./'), port: || 8080, autoOpenBrowser: true, assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/v2': { target: '', changeOrigin: true, pathRewrite: { '^/v2': '/v2' } } }, }
Want to cross-domain requests in
set up:
import axios from 'axios' export const GET_IN_THEATERS = ({ dispatch, state, commit }) => { axios({ url: '/v2/movie/in_theaters' }).then(res => { commit('in_theaters', ) }) }
Used within 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 {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.$('GET_IN_THEATERS') }, mounted () { } } </script> <style lang="scss"> @import "./../../assets/"; @import "./../../assets/"; .movie-page{ padding: 0 rem(40); } </style>
Want to cross domain within components
In Settings:
import axios from 'axios' // Rewrite axios to the prototype property of Vue so that axios can be used in other components.$axios = axios
Settings within 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.$('/v2/movie/in_theaters').then(res => { this.movie_list = }, res => { ('error') }) } } </script> <style lang="scss"> @import "./../../assets/"; @import "./../../assets/"; .movie-page{ padding: 0 rem(40); } </style>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.