Flask接口签名sign原理与实例代码浅析
343
2023-02-14
webpack+vuex+axios 跨域请求数据的示例代码
本文介绍了webpack+vuex+axios 跨域请求数据的示例代码,分享给大家,具体如下:
使用vue-li 构建 webpack项目,修改bulid/config/index.js文件
dev: {
env: require('./dev.envhttp://'),
port: process.env.PORT || 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/v2': {
target: 'http://api.douban.com',
changeOrigin: true,
pathRewrite: {
'^/v2': '/v2'
}
}
},
}
在action.js 中想跨域请求
设置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)
})
}
组件内使BnEyrTJ用:
import {mapState, mapActions, mapGetters} from 'vuex';
import MoviesItem from "./movie-item";
export default {
data () {
return {
}
},
components: {
MoviBnEyrTJesItem
},
computed: {
...mapState({
movie_list: state => {
return state.in_theaters.subjects
}
})
},
methods: {
},
created () {
this.$store.dispatch('GET_IN_THEATERS')
},
mounted () {
}
}
@import "./../../assets/reset.scss";
@import "./../../assets/main.scss";
.movie-page{
padding: 0 rem(40);
}
在组件内想跨域
在main.js设置:
import axios from 'axios'
// 将 axios 改写为 Vue 的原型属性,使在其它的组件中可以使用 axios
Vue.prototype.$axios = axios
在组件内设置:
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 BnEyrTJ=> {
this.movie_list = res.data.subjects
}, res => {
console.infor('error')
})
}
}
@import "./../../assets/reset.scss";
@import "./../../assets/main.scss";
.movie-page{
padding: 0 rem(40);
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~