本文介紹了webpack+vuex+axios 跨域請求數(shù)據(jù)的示例代碼,分享給大家,具體如下:

泌陽網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),泌陽網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為泌陽1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的泌陽做網(wǎng)站的公司定做!
使用vue-li 構(gòu)建 webpack項(xiàng)目,修改bulid/config/index.js文件
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'
}
}
},
}
在action.js 中想跨域請求
設(shè)置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)
})
}
組件內(nèi)使用:
<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>
在組件內(nèi)想跨域
在main.js設(shè)置:
import axios from 'axios' // 將 axios 改寫為 Vue 的原型屬性,使在其它的組件中可以使用 axios Vue.prototype.$axios = axios
在組件內(nèi)設(shè)置:
<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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站題目:webpack+vuex+axios跨域請求數(shù)據(jù)的示例代碼
文章位置:http://www.chinadenli.net/article28/pesscp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站收錄、用戶體驗(yàn)、小程序開發(fā)、網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)