Vue基础3

这篇博客探讨了在Vue框架中进行数据请求的方法,包括原生fetch和axios库的使用,并比较了两者之间的差异。接着,文章介绍了Vue中的计算属性computed和侦听属性watch,阐述了它们在不同业务场景下的应用。最后,详细讨论了Vue的混入mixin功能,以及组件化在解决前端协作和复用问题中的重要性,包括组件的注册、命名规则和使用方式。
摘要由CSDN通过智能技术生成
  1. axios&&fetch
    • 目的:是在框架中使用数据请求
    • 回顾:
      • 封装ajax
      • jquery【get post .ajax .load】
    • 框架
      • 数据请求
      • 1.使用原生js提供的fetch
      • 2.使用第三方封装库:axios
      • 3.fetch vs axios
        • axios对已获得的数据进行了一层封装XSRF
          • axios底层自动对数据进行了格式化
        • fetch并没有进行封装,拿到就是格式化后的数据
          • fetch进行了多一层的格式化
            • res.json()
            • res.blob()格式化二进制
            • res.text()
Axios总结
    1.get方法


    A: 无参数
        axios.get(url).then(res=>console.log(res).catch(error=>conosle.log(error))
    B: 有参数
        axios({
            url: 'http://xxx',
            method: 'get' //默认就是get,这个可以省略,
            params: {
                key: value
            }
        })

    2.post
        注意: axios中post请求如果你直接使用npmjs.com官网文档, 会有坑
        解决步骤: 
                1. 先设置请求头 
                2. 实例化 URLSearchParams的构造器函数得到params对象
                3. 使用params对象身上的append方法进行数据的传参


// 统一设置请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; 
let params = new URLSearchParams()

// params.append(key,value)

params.append('a',1)
params.append('b',2)

axios({
    url: 'http://localhost/post.php',
    method: 'post',
    data: params,
      headers: {  //单个请求设置请求头
       'Content-Type': "application/x-www-form-urlencoded"
    }
})
.then(res => {
    console.log( res )
})
.catch( error => {
    if( error ){
    throw error
}
})


Fetch
1.get

fetch('http://localhost/get.php?a=1&b=2')
    .then(res=> res.text()) // 数据格式化 res.json() res.blob()
    .then(data => {
        console.log( data )
    })
    .catch(error => {
        if( error ){
        throw error
    }
})

注意事项:
    A: fetch 的 get 请求的参数是直接连接在url上的, 我们可以使用Node.js提供的url或是qureystring模块来将
        Object --> String
    B: fetch 的请求返回的是Promise对象,所以我们可以使用.then().catch(),但是要记住.then()至少要写两个, 第一个then是用来格式化数据的,第二个then是可以拿到格式化后的数据
        格式化处理方式有
fetch('./data.json')
.then(res=>{
    res.json() //res.text() res.blob()
})
.then( data => console.log(data))
.catch( error => console.log( error ))


2.post
fetch 文档
https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch#%E8%BF%9B%E8%A1%8C_fetch_%E8%AF%B7%E6%B1%82

fetch项目使用的博客
https://blog.csdn.net/hefeng6500/article/details/81456975
- 历史
    - Vue1.0
        - Vue1.0数据请求我们使用的是一个第三方的封装库,这个封装库叫做vue-resource
        - vue-resource现在已经淘汰了,它的作者也推荐我们使用axios
            - vue-resource使用形式合axios一样的
                - this.$http.get
                - this.$http.post
                - this.$http({})
                - vue-resource有jsonp方法,而axios是没有的
    - Vue2.0
        - axios[可以说是目前最好的数据请求的封装库]
        - fetch
  1. 计算属性
    computed 是Vue中的一个选项
    作用

    • 业务:如果我想让一个字符串反向,如何实现?
      • 分析:反向->数组【reverse】
    <div id="app">
    <p> {
        { msg }} </p>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值