V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
aoscici2000
V2EX  ›  Vue.js

axios 写为 Vue 的原型对象和直接挂载到 window 上有什么区别?

  •  
  •   aoscici2000 · Mar 21, 2019 · 4507 views
    This topic created in 2605 days ago, the information mentioned may be changed or developed.

    如题, 开始设置好 axios 之后就直接写入到 Vue 原型对象上去, 调用时一直就习惯了 this.的方式使用, 后来因为 store 要用也习惯性的 this 调用就出错了, 那如果我一开始就直接设置到 window.axios = axios 上去呢, 二者有什么区别? 感觉用起来也方便很多啊. 为何之前查文档的时候好像没几个这样用的 ?

    4 replies    2019-03-21 23:50:25 +08:00
    noe132
        1
    noe132  
       Mar 21, 2019
    store 也可以加啊。
    import axios from 'axios'

    const store = new Store()
    store.$axios = axios

    像 nuxt.js 这样
    https://github.com/nuxt/nuxt.js/blob/65b8148a3d5ee4782d0cdcb585a3217e4e74d89c/lib/app/index.js#L52
    rawidn
        2
    rawidn  
       Mar 21, 2019 via iPhone
    你觉得在自家拉屎好还是在大街上拉屎好
    uTOmOuk3L6sb4MSI
        3
    uTOmOuk3L6sb4MSI  
       Mar 21, 2019 via iPhone
    akatquas
        4
    akatquas  
       Mar 21, 2019 via iPhone
    因为...没有设计模式

    什么东西都喜欢往全局(不仅仅限于 window )下面挂载...

    缺少分层和模块化
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3044 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 14:01 · PVG 22:01 · LAX 07:01 · JFK 10:01
    ♥ Do have faith in what you're doing.