• Vue(七)——Vue中的Ajax


    目录

    Vue脚手架配置代理

    插槽

    默认插槽

    具名插槽

    作用域插槽


    Vue脚手架配置代理

    本案例需要下载axios库:npm install axios

    1.配置类方式(实现WebMvcConfigurer)  

    2.使用@CrossOrigin注解  

    3.使用nginx反向代理解决跨域  

    4.Vue中配置代理服务器

    代理服务器怎么开启?

    1.ngnix    2.vue-cli

    如何配置代理服务器?

    方法一:

    在vue.config.js中添加如下配置:

    1. devServer:{
    2.   proxy:"http://localhost:5000"

    重启脚手架开启代理 

    说明:

    1. 优点:配置简单,请求资源时直接发给前端(8080)即可。

    2. 缺点:不能配置多个代理,不能灵活的控制请求是否走代理。

    3. 工作方式:若按照上述配置代理,当请求了前端不存在的资源时,那么该请求会转发给服务器 (优先匹配前端资源)

    方法二:

    编写vue.config.js配置具体代理规则:

    1. module.exports = {
    2.    devServer: {
    3.       proxy: {
    4.       '/api1': {// 匹配所有以 '/api1'开头的请求路径
    5.         target: 'http://localhost:5000',// 代理目标的基础路径
    6.         changeOrigin: true,
    7.         pathRewrite: {'^/api1': ''}
    8.       },
    9.       '/api2': {// 匹配所有以 '/api2'开头的请求路径
    10.         target: 'http://localhost:5001',// 代理目标的基础路径
    11.         changeOrigin: true,
    12.         pathRewrite: {'^/api2': ''}
    13.       }
    14.     }
    15.   }
    16. }
    17. /*
    18.    changeOrigin设置为true时,服务器收到的请求头中的host为:localhost:5000
    19.    changeOrigin设置为false时,服务器收到的请求头中的host为:localhost:8080
    20.    changeOrigin默认值为true
    21. */

    说明:

    1. 优点:可以配置多个代理,且可以灵活的控制请求是否走代理。

    2. 缺点:配置略微繁琐,请求资源时必须加前缀。

    vue.config.js

    1. module.exports = {
    2. pages: {
    3. index: {
    4. //入口
    5. entry: 'src/main.js',
    6. },
    7. },
    8. lintOnSave:false, //关闭语法检查
    9. //开启代理服务器(方式一)
    10. /* devServer: {
    11. proxy: 'http://localhost:5000'
    12. }, */
    13. //开启代理服务器(方式二)
    14. devServer: {
    15. proxy: {
    16. '/atguigu': {
    17. target: 'http://localhost:5000',
    18. pathRewrite:{'^/atguigu':''}, // 正则匹配/atguigu变为空字符串,这样真正请求5000的就是/student
    19. // ws: true, //用于支持websocket
    20. // changeOrigin: true //用于控制请求头中的host值
    21. },
    22. '/demo': {
    23. target: 'http://localhost:5001',
    24. pathRewrite:{'^/demo':''},
    25. // ws: true, //用于支持websocket
    26. // changeOrigin: true //用于控制请求头中的host值
    27. }
    28. }
    29. }
    30. }

    插槽

            作用:让父组件可以向子组件指定位置插入html结构,也是一种组件间通信的方式,适用于父组件 ===> 子组件。

    默认插槽

    现在修改需求如下:

    普通解决方案:

    正确解决方案:

    图片写在App的category标签体中 

    此时vue并不知道这个img放在template的哪个位置,所以此时需要用到solt插槽。

    效果:

    Category.vue

    1. <template>
    2. <div class="category">
    3. <h3>{{title}}分类h3>
    4. <slot>我是一些默认值,当使用者没有传递具体结构时,我会出现slot>
    5. div>
    6. template>
    7. <script>
    8. export default {
    9. name:'Category',
    10. props:['title']
    11. }
    12. script>
    13. <style scoped>
    14. .category{
    15. background-color: skyblue;
    16. width: 200px;
    17. height: 300px;
    18. }
    19. h3{
    20. text-align: center;
    21. background-color: orange;
    22. }
    23. video{
    24. width: 100%;
    25. }
    26. img{
    27. width: 100%;
    28. }
    29. style>

    App.vue

    1. <template>
    2. <div class="container">
    3. <Category title="美食" >
    4. <img src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
    5. Category>
    6. <Category title="游戏" >
    7. <ul>
    8. <li v-for="(g,index) in games" :key="index">{{g}}li>
    9. ul>
    10. Category>
    11. <Category title="电影">
    12. <video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4">video>
    13. Category>
    14. div>
    15. template>
    16. <script>
    17. import Category from './components/Category'
    18. export default {
    19. name:'App',
    20. components:{Category},
    21. data() {
    22. return {
    23. foods:['火锅','烧烤','小龙虾','牛排'],
    24. games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
    25. films:['《教父》','《拆弹专家》','《你好,李焕英》','《尚硅谷》']
    26. }
    27. },
    28. }
    29. script>
    30. <style scoped>
    31. .container{
    32. display: flex;
    33. justify-content: space-around;
    34. }
    35. style>

    父组件中:

    1.               <Category>
    2.                  <div>html结构1div>
    3.               Category>

    子组件中:

    1.   <template>
    2.                   <div>
    3.                      
    4.                      <slot>插槽默认内容...slot>
    5.                   div>
    6.               template>

    具名插槽

    出现多个插槽时。需要给每个插槽命名。

    需求如下:红色框一个插槽,蓝色框一个插槽

    此时需要给红色框和蓝色框的元素标签添加solt属性:

    注意如果使用template标签,则写成v-slot

    Catrgory.vue

    1. <template>
    2. <div class="category">
    3. <h3>{{title}}分类h3>
    4. <slot name="center">我是一些默认值,当使用者没有传递具体结构时,我会出现1slot>
    5. <slot name="footer">我是一些默认值,当使用者没有传递具体结构时,我会出现2slot>
    6. div>
    7. template>
    8. <script>
    9. export default {
    10. name:'Category',
    11. props:['title']
    12. }
    13. script>
    14. <style scoped>
    15. .category{
    16. background-color: skyblue;
    17. width: 200px;
    18. height: 300px;
    19. }
    20. h3{
    21. text-align: center;
    22. background-color: orange;
    23. }
    24. video{
    25. width: 100%;
    26. }
    27. img{
    28. width: 100%;
    29. }
    30. style>

    App.vue

    1. <template>
    2. <div class="container">
    3. <Category title="美食" >
    4. <img slot="center" src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
    5. <a slot="footer" href="http://www.atguigu.com">更多美食a>
    6. Category>
    7. <Category title="游戏" >
    8. <ul slot="center">
    9. <li v-for="(g,index) in games" :key="index">{{g}}li>
    10. ul>
    11. <div class="foot" slot="footer">
    12. <a href="http://www.atguigu.com">单机游戏a>
    13. <a href="http://www.atguigu.com">网络游戏a>
    14. div>
    15. Category>
    16. <Category title="电影">
    17. <video slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4">video>
    18. <template v-slot:footer>
    19. <div class="foot">
    20. <a href="http://www.atguigu.com">经典a>
    21. <a href="http://www.atguigu.com">热门a>
    22. <a href="http://www.atguigu.com">推荐a>
    23. div>
    24. <h4>欢迎前来观影h4>
    25. template>
    26. Category>
    27. div>
    28. template>
    29. <script>
    30. import Category from './components/Category'
    31. export default {
    32. name:'App',
    33. components:{Category},
    34. data() {
    35. return {
    36. foods:['火锅','烧烤','小龙虾','牛排'],
    37. games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
    38. films:['《教父》','《拆弹专家》','《你好,李焕英》','《尚硅谷》']
    39. }
    40. },
    41. }
    42. script>
    43. <style scoped>
    44. .container,.foot{
    45. display: flex;
    46. justify-content: space-around;
    47. }
    48. h4{
    49. text-align: center;
    50. }
    51. style>

    父组件中:

    1. <Category>
    2. <template slot="center">
    3. <div>html结构1div>
    4. template>
    5. <template v-slot:footer>
    6. <div>html结构2div>
    7. template>
    8. Category>

    子组件中:

    1. <template>
    2. <div>
    3. <slot name="center">插槽默认内容...slot>
    4. <slot name="footer">插槽默认内容...slot>
    5. div>
    6. template>

    作用域插槽

            理解:数据在组件的自身,但根据数据生成的结构需要组件的使用者来决定。(games数据在Category组件中,但使用数据所遍历出来的结构由App组件决定)

    提出需求: 

    作用域插槽把Category里的games传给App

            谁往插槽里塞结构,那这个games就传给了谁。那App里怎么接收到?要求就是得在要传入的结构的外侧包含template标签,并添加scope属性,其中这个scope属性的值就是所传过来的对象。

    Category.vue

    1. <template>
    2. <div class="category">
    3. <h3>{{title}}分类h3>
    4. <slot :games="games" msg="hello">我是默认的一些内容slot>
    5. div>
    6. template>
    7. <script>
    8. export default {
    9. name:'Category',
    10. props:['title'],
    11. data() {
    12. return {
    13. games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
    14. }
    15. },
    16. }
    17. script>
    18. <style scoped>
    19. .category{
    20. background-color: skyblue;
    21. width: 200px;
    22. height: 300px;
    23. }
    24. h3{
    25. text-align: center;
    26. background-color: orange;
    27. }
    28. video{
    29. width: 100%;
    30. }
    31. img{
    32. width: 100%;
    33. }
    34. style>

    App.vue 

    1. <template>
    2. <div class="container">
    3. <Category title="游戏">
    4. <template scope="atguigu">
    5. <ul>
    6. <li v-for="(g,index) in atguigu.games" :key="index">{{g}}li>
    7. ul>
    8. template>
    9. Category>
    10. <Category title="游戏">
    11. <template scope="{games}">
    12. <ol>
    13. <li style="color:red" v-for="(g,index) in games" :key="index">{{g}}li>
    14. ol>
    15. template>
    16. Category>
    17. <Category title="游戏">
    18. <template slot-scope="{games}">
    19. <h4 v-for="(g,index) in games" :key="index">{{g}}h4>
    20. template>
    21. Category>
    22. div>
    23. template>
    24. <script>
    25. import Category from './components/Category'
    26. export default {
    27. name:'App',
    28. components:{Category},
    29. }
    30. script>
    31. <style scoped>
    32. .container,.foot{
    33. display: flex;
    34. justify-content: space-around;
    35. }
    36. h4{
    37. text-align: center;
    38. }
    39. style>

    父组件:

    1. <Category>
    2. <template scope="scopeData">
    3. <ul>
    4. <li v-for="g in scopeData.games" :key="g">{{g}}li>
    5. ul>
    6. template>
    7. Category>
    8. <Category>
    9. <template slot-scope="scopeData">
    10. <h4 v-for="g in scopeData.games" :key="g">{{g}}h4>
    11. template>
    12. Category>

    子组件:

    1. <template>
    2. <div>
    3. <slot :games="games">slot>
    4. div>
    5. template>
    6. <script>
    7. export default {
    8. name:'Category',
    9. props:['title'],
    10. //数据在子组件自身
    11. data() {
    12. return {
    13. games:['红色警戒','穿越火线','劲舞团','超级玛丽']
    14. }
    15. },
    16. }
    17. script>

  • 相关阅读:
    jvm的结构以及如何调优
    前后端分离项目,vue+uni-app+php+mysql电影院售票系统设计与实现(H5移动项目)
    windows下flutter的环境安装
    web自动化测试(java+seleium)环境安装
    Flink / SQL - 7.一文搞懂常规 Sql TopN 与 Sql Window TopN
    视觉SLAM十四讲:从理论到实践(Chapter5:相机与图像)
    3、从“人等机器”到“人机和谐”
    小谈设计模式(15)—观察者模式
    工业智能网关BL110应用之六: 支持PLC,Modbus,BACnet,电表等协议列表
    C语言——指针进阶(三)
  • 原文地址:https://blog.csdn.net/m0_52601969/article/details/127873512